Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

maximum event re-entrancy depth?

Asked by 7 years ago

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

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

"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.

0
The Errors went away but it did not work killzone45671 -20 — 7y
0
Your code may have more than one mistake, then. BlueTaslem 18071 — 7y
Ad

Answer this question