player.CharacterAdded:Connect(function(char)
local leaderstats = player:WaitForChild("leaderstats")
local stage = leaderstats.Stage
local hum = char:WaitForChild("Humanoid")
task.wait()
char:MoveTo(checkpoints[stage.Value].Position)
hum.Touched:Connect(function(hit)
if hit.Parent == checkpoints then
if tonumber(hit.Name) == stage.Value + 1 then
stage.Value += 1
end
end
end)
end)
Hey, I ran your code and it seems to be working on my end. Are you getting any errors?
Based on my testing there are a few issues that you could run into:
Either the code that you’ve defined inside of your custom PlayerAdded(player) function does not work properly. For example, not creating leader stats properly.
Not setting up the parts inside of checkpoints folder properly. For example, missing the 0’th stage (if your stage leader stat value starts at 0).
Here is the complete script that I used myself to test:
local Players = game:GetService("Players")
local checkpoints = workspace.checkpoints
function createLeaderstats(player: Player): Folder
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats or not leaderstats:IsA("Folder") then
-- Removing duplicate which is not a folder
if leaderstats and not leaderstats:IsA("Folder") then
leaderstats:Destroy()
end
-- Creates a new stage folder
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
return createLeaderstats(player) -- recursive call
else
-- Here, you can create all the leaderstat values
-- that you want to appear
local stages = leaderstats:FindFirstChild("stage")
if not stages then
local stageValue = Instance.new("IntValue")
stageValue.Name = "stage"
stageValue.Parent = leaderstats
end
-- Returning the leaderstats
return leaderstats
end
end
Players.PlayerAdded:Connect(function(player)
createLeaderstats(player)
player.CharacterAdded:Connect(function(character: Model)
local leaderstats = player:WaitForChild("leaderstats")
local stage = leaderstats.stage
local humanoid = character:WaitForChild("Humanoid")
task.wait()
character:MoveTo(checkpoints[stage.Value].Position)
humanoid.Touched:Connect(function(hit)
if hit.Parent == checkpoints then
if tonumber(hit.Name) == stage.Value + 1 then
stage.Value += 1
end
end
end)
end)
end)
Please note that I have renamed your custom PlayerAdded() function into the createLeaderstats() function. I did this to prevent the overlapping name with the Players.PlayerAdded event which might cause confusion.
Here is the place that I have tested it in as well, so you can see that your code actually works leaderstats_testing.rbxl (54.5 KB)
This file contains the same script as I have sent above, but it also has the checkpoint set up in workspace.