Jump to content

Effect Dependent on Nearby Players


Recommended Posts

hia everyone! me again!

I had an idea for a character recently who grows stronger depending on the amount of players or allies nearby. Being a police officer, he has an innate desire to protect others. As such, I was hoping to give him a passive effect which would improve him based on the number of players he can see on screen at any given point. I'm thinking this would increase his defense, allowing him to take less damage when players are around.

I have absolutely no idea how to go about this. I suppose instead of a base number in the character's prefab for his health, I could put a smaller base number and then put a modifier equal to the amount of players nearby? I don't have any idea how to build something like that. Would anyone mind providing some help?

Link to comment
Share on other sites

local function FindPlayers(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, 30, {"player"}, {"playerghost", "INLIMBO"})
    return #ents > 0 and ents or nil
end

local function UpdateStats(inst)
	local pals = FindPlayers()
	if pals ~= nil then
		local health_percent = inst.components.health:GetPercent()
		inst.components.health.maxhealth = 200 * pals
		
		inst.components.health:SetPercent(health_percent)	
	end
end

--in master_postinit
inst:DoPeriodicTask(2, UpdateStats)

I'm very tired so I didn't check to see if this works, but its basically what you're wanting to do:

Every 2 seconds the character will search the area around him in a radius of 30 to find players (dead players do not count, don't remember if you count). If they're any players nearby it will multiply your health by the number of players and scale your current health accordingly. Obviously its OP as is and you should adjust it to what you want it to be.

Link to comment
Share on other sites

On 6/11/2018 at 12:56 AM, DarkKingBoo said:

local function FindPlayers(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, 30, {"player"}, {"playerghost", "INLIMBO"})
    return #ents > 0 and ents or nil
end

local function UpdateStats(inst)
	local pals = FindPlayers()
	if pals ~= nil then
		local health_percent = inst.components.health:GetPercent()
		inst.components.health.maxhealth = 200 * pals
		
		inst.components.health:SetPercent(health_percent)	
	end
end

--in master_postinit
inst:DoPeriodicTask(2, UpdateStats)

I'm very tired so I didn't check to see if this works, but its basically what you're wanting to do:

Every 2 seconds the character will search the area around him in a radius of 30 to find players (dead players do not count, don't remember if you count). If they're any players nearby it will multiply your health by the number of players and scale your current health accordingly. Obviously its OP as is and you should adjust it to what you want it to be.

hia!

this doesn't seem to be working for me. I've copied the top portion and placed it into the location where the maxhealth is normally in the character's prefab, and I placed the bottom part in the master_postinit as instructed. However, whenever I choose the character and enter the world, the game crashes.

I've provided the crash log below. is there anything I'm doing wrong?

thank you so much for creating that code! I am very appreciative of it! ^^

client_log.txt

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...