I need to use wait() before I can index leaderstats. Is this bad?


Basically, I have to use wait() before I can index leaderstats because I store my leaderstats in server storage and parent them to the player in the PlayerAdded() function.
image
Is this bad? I’m pretty sure if a player is laggy this will cause an error. What do I do?

1 Like

What happens if you don’t use the wait(1)? Typically, using WaitForChild is the recommended way to do things like this.

First of all, please post your code as text and use the code formatting button to make it look nice. That works best across different sized monitors and preferences for light mode, dark mode etc. and allows us to copy your code into our editor of choice. Just makes it easier to help in general.

Anyway, yes it’s bad for the reason you mention. The least intrusive fix is to replace wait(1) with repeat wait() until leaderstats. The best way is to not yield at all and use event-based programming instead, which is simpler to make correct. Something like

players.PlayerAdded:Connect(function(player)
    --Setup leaderstats folder
    ...
    --Connect non-display Value objects to display objects.
    --Yes, you can connect functions to events inside functions that are themselves connected to events.
    leaderstats.Cash:GetPropertyChangedSignal("Value"):Connect(function()
        ...
    end)
end)
1 Like

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