#Workspace.WinPart.WinScript:2: attempt to index nil with 'WaitForChild'

8 messages · Page 1 of 1 (latest)

river trellis
#
local player = script:FindFirstAncestorOfClass("Player")
local lb = player:WaitForChild("leaderstats")
local otherstats = player:WaitForChild("otherstats")
local level = lb:WaitForChild("level")
local wins = lb:WaitForChild("wins")
local exp = otherstats:WaitForChild("exp")

script.Parent.Touched:Connect(function(hit)
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    exp.Value = exp.Value + 500
    wins.Value = wins.Value + 1
    print("+1".. exp.Value .."exp and +1".. wins.Value)
end)```
Error l2 "Workspace.WinPart.WinScript:2: attempt to index nil with 'WaitForChild'". 
I don't understand what I'm doing wrong.
#

Oh nvm I understand what's the problem

#

I used findfirstancestorofclass while the script is not located at the good place

#

But then, how can I define player?

#

Since it's a script, I can't use LocalPlayer.

#

I tried this but it's not working.

script.Parent.Touched:Connect(function(hit)
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    
    local pname = hit.Parent.Name
    print(pname)
    
    local player = game.Players.pname
    local lb = player:WaitForChild("leaderstats")
    local otherstats = player:WaitForChild("otherstats")
    
    local level = lb:WaitForChild("level")
    local wins = lb:WaitForChild("wins")
    local exp = otherstats:WaitForChild("exp")
    
    exp.Value = exp.Value + 500
    wins.Value = wins.Value + 1
    print("+1".. exp.Value .."exp and +1".. wins.Value)
end)```
#

"pname is not a valid member of Players"

molten spruce
#

Players[pname]