Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

maximum event re-entrancy depth?

I think the error is because I'm doing something to many times, the error on line 9

local MaxHealth = 120


game.Workspace.ChildAdded:connect(function(Child)
 local Humanoid = Child:FindFirstChild("Humanoid")
 local Player = game.Players:FindFirstChild(Child.Name)
 
 if Humanoid then
  Humanoid.Changed:connect(function()
   if Player and Humanoid.MaxHealth == math.huge then
    Humanoid.MaxHealth = 100
   elseif Player and Humanoid.MaxHealth > MaxHealth then
    Humanoid.MaxHealth = 100
   elseif Player and Humanoid.MaxHealth == 0 and Humanoid.Health > 0 then
    Humanoid.MaxHealth = 100
   elseif Player and Humanoid.Health > MaxHealth then
    Humanoid.Health = 100
   end
  end)
 end
end)

1 answer

✓ Accepted answer

"event re-entrancy" is when an event "re-enters" itself (i.e., it causes itself).

The Changed event can do that by changing the object it's listening to.

Here, you're modifying Humanoid.MaxHealth and Humanoid.Health, which causes the Humanoid.Changed event to begin again.


Don't use game.Players:FindFirstChild. Instead use game.Players:GetPlayerFromCharacter(Child).

Ask if Humanoid and Player then instead of If Humanoid then. You don't need to check Player at all in the event.

Your entire Changed event could be simplified to something like this:

Humanoid.Changed:connect(function()
	if Humanoid.MaxHealth ~= MaxHealth then
		Humanoid.MaxHealth = MaxHealth
	end
end)

Style

It is idiomatic to use lowerCase names for local variables. UpperCase names are used with properties and object names.

It is idiomatic to use ALL_CAPS for constants -- values that don't change -- like MAX_HEALTH.

Use workspace instead of game.Workspace.

2 comments

0

The Errors went away but it did not work

0

Your code may have more than one mistake, then.

This discussion is preserved as it was. New answers, comments, and votes are closed.