Random error happening where it shouldn't?

Hello everyone!

So recently I encountered a problem when coding another tool;

image

the preceding code block;

local players = game:GetService("Players").LocalPlayer
local character = players.Character

repeat wait() until players.Character

local humanoid = character.Humanoid

The confusion I’m facing here, is that the same line of code it’s referring to is used in 6 other tools, while those don’t give me errors. Yet this one does? I’m drawing blanks here, so if anyone knows, please me know!

This has been happening to me a lot too. I think in some cases the script loads faster than the character and all it’s parts, so this error pops up. The thing is, even though I use a WaitForChild(), it still happens.

How about we step through your code:

local character = players.Character

Set character to players.Character, which is currently nil.

repeat wait() until players.Character

Wait until the character exists.

local humanoid = character.Humanoid

Try to index something in character. However, we set character to nil! This is why your script errors.

To fix this, you might do something like:

local character = players.Character or players.CharacterAdded:Wait()

This also removes the need to constantly check if the character exists or not. Polling is very bad. You should use events instead.

2 Likes

This half-fixed the problem. It ended up trying to look for Character inside the player model. Which I don’t quite understand how that happened. I ended up just switching where the wait was and it’s fixed. Thanks!

2 Likes