Checkpoint not working

Hi, what i have set up is if the player joins the game, a intvalue is added and is set to 1. When the player touches the checkpoint, it can’t find it, hence why this script won’t work:

local Checkpoint = script.Parent
local CheckpointNumber = Checkpoint.Name

local function onPartTouched()
	local player = game.Players.LocalPlayer
	local CurrentCheckpoint = player:FindFirstChild("CurrentCheckpointNumber").Value
	
	if CheckpointNumber == CurrentCheckpoint then
		CurrentCheckpoint = CurrentCheckpoint + 1
	end
end

Checkpoint.Touched:Connect(onPartTouched())

Can we see the game.Players.PlayerAdded script if you have one? It might not be working because you’re doing things before that

game.Players.ChildAdded:Connect(function(player)
	local CheckpointNumber = Instance.new("IntValue")
	CheckpointNumber.Name = "CurrentCheckpointNumber"
	CheckpointNumber.Value = 1
	CheckpointNumber.Parent = player
end)

Oh, you should use PlayerAdded instead. Also is that a server script or local script, and where’d you put it?

Still giving me an error with this line

What does the error say? Is it nil?

Workspace.Checkpoints.1.Script:6: attempt to index nil with ‘FindFirstChild’

game.Players.PlayerAdded:Connect(function(player)
Are you doing anything before this line?

Yes, I am. I changed ChildAdded to Playeradded.

1 Like

No I meant like are you putting any lines before the playeradded function?

No, it’d in a different script. The checkpoint script is inside of the checkpoint part as the plsyeradded script is inside of serverscriptservice

Oh, I see the problem. You’re trying to get a localplayer out of the player who stepped on the checkpoint. You can’t do that since the script is not in the player. What you should do is use this function: game.Players:GetPlayerFromCharacter(player's character)
Then find the intValue in them. You can get the player’s character by adding a parameter in the onPartTouched() function

So what should the argument be?

local Checkpoint = script.Parent
local CheckpointNumber = Checkpoint.Name

local function onPartTouched(person)
  if person.Parent:FindFirstChild("Humanoid") then
	local player = game.Players:GetPlayerFromCharacter(person.Parent)
	local CurrentCheckpoint = player:FindFirstChild("CurrentCheckpointNumber")
	
	if tonumber(CheckpointNumber) == CurrentCheckpoint then
		CurrentCheckpoint.Value = CurrentCheckpoint.Value + 1
        end
	end
end

Checkpoint.Touched:Connect(onPartTouched())

Whoops, add a tonumber for the checkpoint number if statement because you’re using a string
edit: I edited the original script

It seems the intvalue isn’t changing…?

Here’s what I did in RS and it worked.

local part = script.Parent
local num = workspace:FindFirstChild("YOURCHECKPOINTNAME").Name
local db = false

part.Touched:Connect(function(person)
	if game.Players:GetPlayerFromCharacter(person.Parent) and not db then
		db = true
		local plr = game.Players:GetPlayerFromCharacter(person.Parent)
		local intvalue = plr:FindFirstChild("YOURINTVALUE")
		intvalue.Value = tonumber(num)
		wait(1)
		db = false
	end
end)

Make sure the script inside the checkpoint is a regular script

local checkpointDir = workspace.Checkpoints

function setupStats(plr)
local stats = Instance.new(“Folder”)
stats.Name = “leaderstats”
local stage = Instance.new(“NumberValue”)
stage.Name = “Stage”
stage.Value = 1
stats.Parent = plr
stage.Parent = stats
end

function AdvanceStage(plr,checkpoint)
local p = game.Players:GetPlayerFromCharacter(plr)
local stage = p:FindFirstChild(‘leaderstats’):FindFirstChild(‘Stage’)
if p and stage~=nil and plr:FindFirstChild(‘Humanoid’).Health>0 then
if(stage.Value+1) == tonumber(checkpoint) then
stage.Value = stage.Value+1
end
else
return
end
end

game.Players.PlayerAdded:connect(function(p)
setupStats(p)
p.CharacterAdded:connect(function(c)
local curStage = p:FindFirstChild(‘leaderstats’):FindFirstChild(‘Stage’)
if curStage~=nil and checkpointDir:FindFirstChild(tostring(curStage.Value)) then
local point = checkpointDir:FindFirstChild(tostring(curStage.Value))
wait(.3)
c:WaitForChild(‘HumanoidRootPart’).CFrame = CFrame.new(point.Position+Vector3.new(0,5,0))
end
end)
end)

for i,v in pairs(checkpointDir:GetChildren()) do
v.Touched:connect(function(h)
if h.Parent:FindFirstChild(‘Humanoid’) then
AdvanceStage(h.Parent,v.Name)
end
end)
end

The intvalue still didn’t change

I edited the script, try putting it in. Also please send what you have rn