I have to wait 5 seconds before running a script that animates?

Why do I have to do this?

wait(5)
local player = script.Parent.Parent.Parent
local character = player.Character or player.CharacterAdded:Wait()

if not character.Parent then -- this part doesn't even work properly for some reason so i have to wait 5
	repeat wait() character = player.Character until character.Parent
end

Why 5 seconds? I don’t thin the wait(5) is necessary at all, but if it is, change it to a lower number. Otherwise, explain more on your problem.

1 Like

What i’m trying to explain is if there’s a better method than waiting a specific amount of seconds before running the code?

Can you show what your explorer looks like during runtime. Specifically open all the folders in your player’s Instance.

Sure, PlayerScripts just has regular stuff I never added anything there. Here’s what my explorer looks like ingame (for my player):
image

Is this what you wanted to see? @xZylter

Yes,

Using script.Parent.Parent.Parent is indexing the Players container. Remove one of your Parent statements. For LocalScripts you can simply do game.Players.LocalPlayer and get your player Instance, much easier method.

Personally, I would try and avoid using script.Parent as a method to index since it makes code harder to understand.

I’m confused on how this is the issue though, because that would mean my whole script doesn’t work even though it does. I merely just have to wait 5 seconds for some reason. Also the script in Backpack called LocalScript has nothing to to with the problem it’s a totally different script.

Here’s what is inside of Sword:
image

So which script is the script your trying to run and needs the wait(5)?

The script that is having the animation loading error is the ServerResponse script. Also it has to be server sided, because my game is ping based.

I still have a poor understanding of what the problem is, however, if it’s an issue with instances not loading in time then try to change the variables referencing the instances to :WaitForChild and then it should wait until they’re loaded before it executes.

1 Like

I’ve already tried that, so I resorted to creating Animations inside of a script, and just returning them through either a module, or bindablefunction.

@xWalsh Do you know what the problem could be? Also this post is about an alternative to waiting until all animations are fully loaded, because if I were to remove the wait(5) seconds before executing the code, the script would error.

Have you tried this? You never responded.

1 Like

My bad yeah that works, the main problem I was having was that I was running it on the server however when you click (from localscript), it fires to the server so how I fixed it was also by adding that same loop inside the local script before executing that code. that fixed it entirely so now it works!

@giorgi311 scratch that it works, but still errors occasionally.

repeat wait() until char

This will yield the entire current thread until the char variable returns a variable.