Wait, why is this a thing?

So i was checking the error logs on my game and i found this


Why is this a thing? The event is under control and its only fied at most once every 10 seconds. This makes it significantly harder to make recursive things. i know i can use a loop but that doesnt fit within my scenario. due to this i might have to rework some other scripts aswell.

My code that uses the event goes something like this:

script.BindableEvent.Event:Connect(function()
  game.Players.LocalPlayer.Character:WaitForChild("Humanoid") --Ensure the character has loaded
  game.PLayers.LocalPlayer.Character.Humanoid.Died:Wait()
  --Do some camera transitions here
  script.BindableEvent:Fire()
end)
2 Likes

Why use recursion here? Roblox has to stop the stack somewhere.

while true do
    humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
    humanoid.Died:Wait()
    -- Do stuff
end

yes, i know theres other ways to do it. but i dont see why it wouldnt work, why would it need to create a stack as you said? its not like the it should wait for the event to return anything since well, itss an event not a function

If this BindableEvent only handles camera animation when a player dies, you can instead do this logic in a function that’s ran for every new character model for a player. No event re-entrancy or loop needed. This situation doesn’t benefit from recursion.

The re-entrancy limit prevents unbroken recursion from obliterating memory and execution time. If you encounter this in an another implementation that needs recursion, avoid re-entrancy.

The interpreter doesn’t know what it does or doesn’t need. It could be really important that the code connected to the event finish up before it moves on, so it needs the stack to be sure. Comparable code:

function thing()
	task.wait()
	print("thing")
	thing()
end
thing() -- kills performance

still feels kinda weird since events dont even yield but alr

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.