Variable not updating / i don't really know what to do!

I’m trying to make a checkpoint system with leaderstats but the checkpoint instance variable isn’t updating. leaderstats are made on server btw

Script:

local plr = game.Players.LocalPlayer
local current = 0
local maxcheck = 0
for _,v in pairs(workspace.Loaded.CheckPoints:GetChildren()) do maxcheck = maxcheck + 1 end
local indicator = workspace:WaitForChild("Arrow")
local checkpoints = workspace.Loaded.CheckPoints
local deb = false
local checkpoint = nil
checkpoint = checkpoints:FindFirstChild("CheckPoint"..current+1)
indicator.Position = checkpoint:WaitForChild("CheckPoint").Position
checkpoint:FindFirstChild("Hitbox").Touched:Connect(function(hit)
	if hit.Parent == plr.Character and plr.Character:FindFirstChildOfClass("Humanoid").Health > 0 and deb == false then
		if current < maxcheck then
			deb = true
			current += 1
			checkpoint = checkpoints:FindFirstChild("CheckPoint"..current)
			indicator.Position = checkpoint:WaitForChild("CheckPoint").Position
			game.ReplicatedStorage.NextStage:FireServer()
			wait(0.1)
			deb = false
		end
	end
end)

Post been quiet for 15 whole hours :/.

The only time the checkpoint variable is updated to a new object is when the script starts for the first time.

If you want checkpoint to be updated when the current variable is increased by 1, then that function would need to reference the checkpoint variable again and update it to the desired new value.

Wait, sorry forget to remove comment before the line that updates in the .Touched.

1 Like

Still doesn’t work tough at all tough

I assume the problem also isn’t so clear cuz i made the post when i was pretty tired.
So the problem is the script only detects .Touched on the first checkpoint the indicator goes to the right one but it only detects .Touched on checkpoint number 1

1 Like

Ah, that’s probably happening because when the function was created, it was referring to the Hitbox of the original checkpoint object. Even though the variable is updated after the fact, the function is still referring to that original checkpoint.

A possible solution for that would be to restructure the function in a way where the Touched event is disconnected for the old checkpoint when current is increased by 1, and then to connect the event to the function for the newly assigned checkpoint.

I’ll provide an example revision soon

I’ve gone into Roblox Studio and tested this out as best I can (based on the context provided from the original code), so this’ll hopefully end up working right away without any issues.

I did adjust the names of some of the variables to make it easier for me to know what everything was referring to while restructuring the code, so if you’d like clarification about anything, feel free to ask!

Example Revision

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local NextStage = ReplicatedStorage:WaitForChild("NextStage")

---

local indicator = workspace:WaitForChild("Arrow")
local Loaded = workspace:WaitForChild("Loaded")
local CheckPoints = Loaded:WaitForChild("CheckPoints")

local currentGoal = 1
local maxcheck = 0

CheckPoints.ChildAdded:Connect(function()
	maxcheck += 1
--[[
In case any of the checkpoints hadn't replicated to the client yet,
this will ensure that "maxcheck" is updated to account for that.

However, if your game has StreamingEnabled turned on, this could cause
issues where some checkpoints are streamed out, then streamed back in,
causing "maxcheck" to increase again. As a result, I'd recommend defining
maxcheck somewhere on the server-side (maybe an Attribute) that this
client-sided script could refer to to always have the most accurate idea
of how many checkpoints exist
--]]
end)

for _,v in CheckPoints:GetChildren() do
	maxcheck += 1
end

---

local deb = false
local goalCheckpoint = nil

goalCheckpoint = CheckPoints:FindFirstChild("CheckPoint"..currentGoal)
indicator.Position = goalCheckpoint:WaitForChild("CheckPoint").Position


local connection

local function checkpointTouchedFunction()
	deb = false
	local Hitbox = goalCheckpoint:WaitForChild("Hitbox")
	
	local updatingToNewCheckpoint = false
	connection = Hitbox.Touched:Connect(function(hit)
		if (currentGoal - 1) >= maxcheck then return end
		if deb == true then return end
		
		deb = true
		
		local modelCheck = hit:FindFirstAncestorOfClass("Model")
		local Character = player.Character or player.CharacterAdded:Wait()
		
		if modelCheck == Character then
			local Humanoid = Character:WaitForChild("Humanoid")
			if Humanoid.Health <= 0 then return end
			
			updatingToNewCheckpoint = true
			currentGoal += 1
			
			connection:Disconnect()
			connection = nil
			
			local newCheckPointCheck = CheckPoints:FindFirstChild("CheckPoint"..currentGoal)
			if not newCheckPointCheck then
				warn("Cannot find next CheckPoint")
			else
				goalCheckpoint = newCheckPointCheck
				
				indicator.Position = goalCheckpoint:WaitForChild("CheckPoint").Position
				NextStage:FireServer()
				
				checkpointTouchedFunction()
			end
		end
		
		if updatingToNewCheckpoint == false then
			deb = false
		end
		
	end)
	
end
checkpointTouchedFunction(goalCheckpoint)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.