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

Attempt to index nil with waitforchild on a npc?

Asked by 3 years ago

Attempt to index nil with waitforchild on a npc and this is a reward script here's my code

local Player = game.Players.LocalPlayer
local Leaderstat = Player:WaitForChild("leaderstats")
local Humanoid = script.Parent.Humanoid
function Dead() 
    Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 100
wait(0.1) 
script:remove()
end 
Humanoid.Died:connect(Dead) 
0
On which line does it occur?? And also, is this a local or server script? User#32819 0 — 3y
0
a server script and on the leaderstat part Agent0fChaos0 14 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

You said this was a server script, okay. So the problem is, you can't do game.Players.LocalPlayer on a server script. A workaround around this is the .PlayerAdded event, wich returns the player as an argument. Here is your script rewritten:

game.Players.PlayerAdded:Connect(function(player) -- this will be player instead of using LocalPlayer

    local Leaderstats = player:WaitForChild("leaderstats")
    local Humanoid = script.Parent.Humanoid

    function Dead()

        Leaderstats.Cash.Value += 100 -- made some changes so its simpler
        wait(0.1) 
        script:Destroy()

    end 

    Humanoid.Died:Connect(Dead) 

end)
0
:0 it worked! thank you! Agent0fChaos0 14 — 3y
0
Oh wait it only worked once Agent0fChaos0 14 — 3y
0
and also it says dead is a global consider changing it to local Agent0fChaos0 14 — 3y
0
I only worked once because you destroy the script duh User#32819 0 — 3y
Ad

Answer this question