Checkpoint glitching out at random stages

Hey,

Recently I’ve found a major bug in my obby. at random stages, the checkpoints will begin glitching out. The problem is, I cannot find where the issue is. My obby is a randomly generating obby that won’t end, so I’m not sure if that’s the issue.

Issue (Video is cut to the time of the issue)

Checkpoint script

local Checkpoint = script.Parent

Checkpoint.Touched:Connect(function(TouchPart)
	if TouchPart.Parent:FindFirstChild("Humanoid") ~= nil then
		local Player = game.Players:GetPlayerFromCharacter(TouchPart.Parent)
		local PlayerData = game.ServerStorage:FindFirstChild("PlayerCheckpointData")

		if PlayerData == nil then
			PlayerData = Instance.new("Model", game.ServerStorage)
			PlayerData.Name = "PlayerCheckpointData"
		end

		local CheckpointData = PlayerData:FindFirstChild(Player.UserId)

		if CheckpointData == nil then
			CheckpointData = Instance.new("ObjectValue", PlayerData)
			CheckpointData.Name = Player.UserId
			CheckpointData.Value = Checkpoint
		else
			CheckpointData.Value = Checkpoint
			Player.Character:WaitForChild("Humanoid").Died:Connect(function()
				wait()
				Player.Character:WaitForChild("HumanoidRootPart").CFrame = CheckpointData.Value.CFrame + Vector3.new(0,4,0)			
			end)
		end
	end
end)

First thing, in the checkpoint touched event you’ll want to add a debounce, because when any part of the player touches it, it’ll fire. So it could fire many times a second (every time the player moves, multiple body parts will hit it)

Adding a debounce is simple you could just create a table

local debounce = {} -- has to be a table since there are multiple players

part.Touched:Connect(function(hit)
   local player = game.Players:GetPlayerFromCharacter(hit.Parent)
   if not debounce[player] then
        debounce[player] = true

        -- rest of the code
   else
       -- player already touched checkpoint
   end

end)

Since you don’t have a debounce it’s connecting a ton of Humanoid.Died events. Speaking of the Humanoid.Died event you should probably put that out of the Touched event so it doesn’t connect multiple times.


Other than that I can’t figure out why the player is teleporting to the previous stage. If there are any other scripts that teleport the player can you provide us with them?

1 Like

okay so ima point out the 1st problem i see. alright so in ur checkpoint script i see no debounce so the .Touched event is firing very frequently. so probably try adding a debounce. also in the line

right here you used the .Died function. this function is fired once the character dies. so if you teleport the player’s HumanoidRootPart it wont teleport the whole player’s character since the player’s is already in pieces (if the DestroyOnDeath is on) so my solution for this try teleporting the player’s character when the character spawns not when it dies. so use .CharacterAdded

also i recommend checking if the player is in the correct stage before setting it to the checkpoint. you can do this by using an if statement and checking if the PastCheckPoint + 1 will equal to the NextCheckPoint

here’s an example:

local CheckPoints = {"CheckPoint 1", "CheckPoint 2", "CheckPoint3"} --the checkpoints 
local Debounce = true

Part.Touched:Connect(function(Hit) --Part being the touched CheckPoint
	if Debounce == true then
		Debounce = false
		if Hit.Parent:FindFirstChild("Humanoid") then
			local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)

			if Player then
				local CheckPointData = Player.CurrentCheckPoint -- the CheckPointData
				local PastCheckpointNumber = tonumber(string.sub(CheckPointData.Value.Name, 11, 12)) --gives the number in the CheckPoint like the number 1 in "CheckPoint 1"
				local NextCheckpointNumber = tonumber(string.sub(Part.Name, 11, 12)) --this is the current chekpoint number that is being touched so if this is the next stage then this checkpoint will be "CheckPoint 2"

				if PastCheckpointNumber ~= nil and NextCheckPointNumber ~= nil then
					if PastCheckpointNumber + 1 == NextCheckPointNumber then
						--the player is in the correct next stage.
						CheckPointData.Value = Part
						print("Updated  the player's checkpoint")
					else
						print("i sense hacks >:0")
					end
				else
					warn("one of the CheckPoint number turned back nil??? wait wut u gotta fix that")
				end
			end
		end
		wait(2)
		Debounce = true
	end
end)

this script makes sure the player’s next stage is the correct one and wont change it untill it is the correct one.

keep note: the script i put there is just an example so if u copy an paste it, it wont work. i just made it so u can understand what i mean by how u make it.

3 Likes

Ohh, that’s what happened! Thanks for the help; and also thanks to @elonrocket!

1 Like