Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to check LocalPlayer WalkSpeed properly?

Hi there! I want an icon to show up and disappear (using ImageTransparency) when a player has a certain speed (in this case WalkSpeed must be 16).

local Player = game.Players.LocalPlayer
local Character = Player.Character
	if Character.Humanoid.WalkSpeed == 16 then
		script.Parent.ImageTransparency = 0
end		
	if Character.Humanoid.WalkSpeed == 11 then
		script.Parent.ImageTransparency = 1		
end

This however.. gives me the following error:

Players.baukeblox12.PlayerGui.Stamina.Frame.SprintImage.LocalScript:5: attempt to index nil with 'Humanoid'

I am new to Roblox Development! So some explanation would be appreciated! :D

1 answer

✓ Accepted answer

Either Character or Humanoid is loading too late. This happens super often; when the script loads before the player. Here are some modifications you can make.

local Character = Player.CharacterAdded:Wait() or Player.Character --This makes you wait for the Character to load, and then defines the Character

local Humanoid = Character:WaitForChild("Humanoid") --This waits for the Humanoid to spawn before defining it.

Hopefully this helped.

4 comments

0

Thanks for the help! I don't understand some code here.. Why do you add or Player.Character because you know that Player.Character will not work anyway

0

So what the code is doing is basically choosing whichever one works. Player.Character or Player.CbaracterAdded. Since one of them works; Player.CharacterAdded:Wait(), it waits for the Character to load. Character Added isn't the same as Player.Character. The code is checking to see if one or the other gives an error, so since CharacterAdded will give an error later on, you put or Player.Character

0

Basically, Player.CharacterAdded isn't the same thing, it just tells it to wait for the Character to be added, then you later define what character really is.

0

Apologies if you don't understand what I mean. I'm super tired and also on mobile.

This discussion is preserved as it was. New answers, comments, and votes are closed.