#Why is my script giving an error

1 messages · Page 1 of 1 (latest)

loud stump
#

This is the error I got: ServerScriptService.CheckPointScript:23: attempt to index nil with 'leaderstats' - Server - CheckPointScript:23
This is the script below. Please and Thank you.

local Players = game:GetService("Players")

local CheckpointsFolder = game.Workspace.CheckPoint

Players.PlayerAdded:Connect(function(player)

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

local Stage = Instance.new("IntValue")
Stage.Name = "Stage"
Stage.Parent = leaderstats

end)

for _, v in pairs(CheckpointsFolder:GetChildren()) do

if v:isA("BasePart") then
    v.Touched:Connect(function(part)
        
        local player = Players:GetPlayerFromCharacter(part.Parent)
        local stageValue = player.leaderstats.Stage        
        
        if player and stageValue.Value < tonumber(v.Name) then
            
            stageValue.Value = v.Name
            
        end
    end)
end

end

celest thunder
#

Part can be touched by many many other things

#

that aint player

#

makes player be nil