Problematic Code

Hey, I’m trying to write a script for my obby that makes all previous stages switch materials to neon. It should trigger when the player joins or their stage value changes. However, upon running the script, only two stages behind me, 99 and 98, light up. My checkpoints are properly categorized and named, and the player’s stage value is properly set up. I asked ChatGPT for this script, and I don’t know how to fix it. Help, please?

-- Variables
local player = game.Players.LocalPlayer
local checkpointsFolder = workspace:WaitForChild("checkpoints")  -- Folder with checkpoints

-- Function to update checkpoints
local function updateCheckpoints()
	-- Access the player's leaderstats
	local leaderstats = player:WaitForChild("leaderstats", 10)  -- Wait for leaderstats to load
	if not leaderstats then
		warn("Leaderstats not found for player: " .. player.Name)
		return
	end

	-- Access the player's current stage
	local currentStage = leaderstats:WaitForChild("stage", 10)  -- Wait for Stage value
	if not currentStage then
		warn("Stage not found in leaderstats for player: " .. player.Name)
		return
	end

	-- Loop through all checkpoints in the folder
	for _, checkpoint in pairs(checkpointsFolder:GetChildren()) do
		if checkpoint:IsA("BasePart") then  -- Ensure it's a BasePart
			local checkpointNumber = tonumber(checkpoint.Name)  -- Convert the checkpoint's name to a number

			if checkpointNumber then
				-- If checkpoint number is less than the player's current stage, set it to Neon
				if checkpointNumber <= currentStage.Value then
					checkpoint.Material = Enum.Material.Neon
				else
					-- Reset to Plastic if it's greater than or equal to the current stage
					checkpoint.Material = Enum.Material.Plastic
				end
			else
				warn("Checkpoint name '" .. checkpoint.Name .. "' is not a valid number")
			end
		end
	end
end

-- Trigger the update when the player spawns or the stage changes
local function onCharacterSpawned()
	-- Initial update when the player spawns
	updateCheckpoints()

	-- Update whenever the player's stage changes
	local leaderstats = player:WaitForChild("leaderstats", 10)
	if leaderstats then
		local stage = leaderstats:WaitForChild("Stage", 10)
		if stage then
			stage.Changed:Connect(function()
				updateCheckpoints()  -- Update checkpoints whenever the stage changes
			end)
		end
	end
end

-- Listen for the player's character to spawn (initial trigger)
player.CharacterAdded:Connect(onCharacterSpawned)

-- If the player's character is already present (handles edge cases)
if player.Character then
	onCharacterSpawned()
end

I don’t see any indication of you using DataStores to save the player’s stage value when they leave. I’ll leave a topic here for you to get started on it. Meanwhile let me rewrite your code

local player = game.Players.LocalPlayer
local checkpointsFolder = workspace.checkpoints
function UpdateCheckpoints()
   local currentstage = player.leaderstats.Stage.Value
   for i, checkpoint : BasePart in pairs(checkpointsFolder:GetChildren()) do
      if tonumber(checkpoint.Name) <= currentstage then
          checkpoint. Material = "Neon"
      else
          checkpoint. Material = "Plastic"
      end
   end
end
function CharacterSpawned()
   updateCheckpoints()
   player.leaderstats.Stage.Changed:Connect(updateCheckpoints)
end
player.CharacterAdded:Connect(CharacterSpawned)

1 Like

Thanks! I am using a DataStore to save the player’s stage data.

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