Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to check LocalPlayer WalkSpeed properly?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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.

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 baukeblox12 4 — 4y
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 DaBagelBoy 90 — 4y
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. DaBagelBoy 90 — 4y
0
Apologies if you don't understand what I mean. I'm super tired and also on mobile. DaBagelBoy 90 — 4y
Ad

Answer this question