2

I can not resolve this by my self.

Thank You for helping.

This is my first stackoverflow question :3

Roblox Studio information: Latest available version as of 2022 October 03 Picture of about window: https://i.stack.imgur.com/Wo6Br.png https://i.stack.imgur.com/v7Jot.png

The output: ServerScriptService.CheckpointsScript:31: attempt to index nil with 'leaderstats'

The code:

local Players = game:GetService("Players")

local CheckpointsFolder = game.Workspace.Checkpoints

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
    
    --player.CharacterAdded:Connect(function(char)
        
    --  local checkpoint = CheckpointsFolder:FindFirstChild(Stage.Value)
        
    --  player.Character..CFrame = CFrame.new(checkpoint.PrimaryPart.CFrame.Position.Y + 5 =, checkpoint.PrimaryPart.CFrame.Rotation)
        
    --end)
    
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
aynber
  • 22,380
  • 8
  • 50
  • 63

1 Answers1

2

Your issue is coming from this line

local stageValue = player.leaderstats.Stage

And that is because the Touched event fires for any object that touches it. And you need to make sure that player actually exists.

You are doing this already, you just need to move this line inside your safety check.

            local player = Players:GetPlayerFromCharacter(part.Parent)
            if player then
                local stage = player.leaderstats.Stage
                local currentStage = tonumber(v.Name)
                if stage.Value < currentStage then
                    stage.Value = currentStage 
                end
            end
Kylaaa
  • 6,349
  • 2
  • 16
  • 27