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

Why is my leaderstats script is saying there is a nil value?

Asked by 2 years ago

I have made a script that I think should show the players speed in the leaderstats. But the script does not even work. The error is: "ServerScriptService.Script:3: attempt to call a nil value" Here's they script:

game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.New("Folder",player)
    leaderstats.Name = "leaderstats"

    local Speed = Instance.New("IntValue", leaderstats)
    Speed.Name = leaderstats

    while true do
        wait()
        Speed = player.Character.Humanoid.WalkSpeed
    end
end)

I hope you can help! I should see it soon if you post an answer in the next 2 hours so please leave one!

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

There are so many errors in this script. In line 3 and 6, always use Instance.new() not Instance.New(). In line 7 you're naming an IntValue an instance instead of a string (string is a group of words inside two quotation marks; they usually appear green when coding), which will cause errors. And lastly, in line 11, you're trying to change an instance into a number, which will cause AN ERROR. Instead, use Speed.Value.

This is the fixed script:

game.Players.PlayerAdded:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()

    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local Speed = Instance.new("IntValue", leaderstats)
    Speed.Name = "Speed"

    while true do
        task.wait()
        Speed.Value = character:FindFirstChildOfClass("Humanoid").WalkSpeed
    end
end)

Hopefully this script works!

If you feel discouraged while reading my answer, don't be. I'm not mad, I'm helping you with your script. I understand you're new to scripting and that's fine. I hope you will get better and have a nice day!

0
I tried this and it didn't work... Puppy_lovertheawsome 84 — 2y
0
wdym? are you using a local script or a normal script? this only works in normal scripts.. T3_MasterGamer 2189 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

This is my leaderstats script you should be able to work your variables into this

game.Players.PlayerAdded:Connect(function (plr)
    local stats = Instance.new("BoolValue",plr)
    stats.Name = "leaderstats"

    local cash = Instance.new("IntValue",stats)
    cash.Name = "Credits" 
    cash.Value = 500 
end)

I think your issue is Line 7 when your giving Speed the name of leaderstats. also Speed is the name of the currency or stat so you wouldent want speed to = player.Character.Humanoid.WalkSpeed

it should be Speed.Value = player.Character.Humanoid.WalkSpeed

Im newer to scripting but I hope this helps you out!

0
thanks I will try it! Puppy_lovertheawsome 84 — 2y
0
So I changed The line 3 to the BoolValue and it still says the same error as shown in my question. Puppy_lovertheawsome 84 — 2y
0
should this also be a local script? Puppy_lovertheawsome 84 — 2y

Answer this question