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

easy beginner's script doesn't work cause of (a nil value) error..why this happens ?

Asked by
hmurban 16
5 years ago

server-sided script in serverscriptservice it say in the output that there is a proplem in line 3 attempt to index a local "char" (a nil value) it's sooo strange..that script should be very easy and it worked well before today !

game.Players.PlayerAdded:Connect(function(plr)
    local char = plr.Character
    local human = char.Humanoid
    human.WalkSpeed = 100

    end)

when i changed the script as this it worked again !!

game.Players.PlayerAdded:Connect(function(plr)
    local char = game.Workspace:WaitForChild("hmurban")
    local human = char.Humanoid
    human.WalkSpeed = 100

    end)

1 answer

Log in to vote
0
Answered by
Pojoto 329 Moderation Voter
5 years ago

You need to make sure the character has loaded in. What's happening is that when the player joins, you're trying to access the player's character before it even gets a chance to exist.

To make this work we need to use this:

Players.CharacterAdded:Connect(function(character)

This runs the function when the character loads in. I'm not going to show you how to use it, I'm actually going to challenge you to try to make this work on your own. If you need help just ask in the comments.(I'm not sure if I'm allowed to do this :P)

0
oh i didn't know about that CharacterAdded event :0 hmurban 16 — 5y
0
CharacterAdded isn't a valid member of Players... that's why i didn't know about it XD thanks for reblaying ^^ hmurban 16 — 5y
0
No worries! :) Pojoto 329 — 5y
Ad

Answer this question