There's a method you can use called WaitForChild to wait until the Humanoid becomes available.
What's WaitForChild?
WaitForChild does exactly what it says on the tin. The script yields (waits) until the child you specify becomes available. That way, there's no issues in other parts of the script where you need to reference that child.
Here's an example of how to use this method:
plr = game.Players.LocalPlayer
plr.Character:WaitForChild("Humanoid")
Optimizing your code:
Right now, you're using an infinite loop to do what you're trying to do. However, there's a much easier way to do what you're trying to do. You can use a combination of two events called PlayerAdded and CharacterAdded to make it so the player's humanoid always changes name when their character is added or respawned. Plus, you don't need multiple scripts that are replicated to each player, it's all handled in one script.
Firstly, make a new script in ServerScriptService. Then, you'll want to do the following:
game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded:connect(function(char)
char:WaitForChild("Humanoid").Name = "antireset"
end)
end)
Bare in mind that the PlayerAdded event does not work in Play Solo. I would suggest starting up a local Test Server (it's under the Test tab in Studio) to see if it works.
You can get rid of the comments, but they are there to show you how the script works. I hope this helps you.
Read more on anonymous functions here.
6 comments
Thx! I already know how to use anonymous functions tho.
ServerScriptStorage..? Do you mean ServerScriptService?
Edited my answer, thanks for the correction.
BTW, if my answer helped you, click the Accept Answer button next to my answer. :)
oops my bad i was typing and didn't realize someone had already answered it.
No problem, I've done that plenty of times.