Need help with my teleport to checkpoint system

Hello! I’m stuck with teleport system. Basically, I made a checkpoint system that if the player touched the water, it teleport them back to the checkpoint. I also had a matchmaking system, that whenever the match start, it will delete all the checkpointData. The system worked but it a bit glitchy, sometimes, the player touched the water but it didn’t teleport the player back, after like 5-8 seconds, it teleported. I don’t know where the issue, the output didn’t show any errors.
This is the checkpoint script.

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

local checkpoint = script.Parent

function onTouched(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		local checkpointData = ServerStorage:FindFirstChild("CheckpointData")
		if not checkpointData then
			checkpointData = Instance.new("Folder")
			checkpointData.Name = "CheckpointData"
			checkpointData.Parent = ServerStorage
		end

		local userIdString = tostring(player.UserId)
		local checkpointValue = checkpointData:FindFirstChild(userIdString)
		if not checkpointValue then
			checkpointValue = Instance.new("ObjectValue")
			checkpointValue.Name = userIdString
			checkpointValue.Parent = checkpointData

		end

		checkpointValue.Value = checkpoint
	end
end

checkpoint.Touched:Connect(onTouched)

This is the explorer.
image
This is the water script.

local rep = game:GetService("ReplicatedStorage")
local storage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local debounce = false
local cd = 0.5

function onTouch(hit) 
	if not debounce then			
		
		local humanoid = hit.Parent:FindFirstChild("Humanoid") 
		
		debounce = true
		
		if (humanoid ~= nil) and (humanoid.Health ~= 0) then	-- if a humanoid exists, then			
			local char = hit.Parent
			local player = Players:GetPlayerFromCharacter(char)		
			
			local userIdString = tostring(player.UserId)	
			
			local checkpointData = storage:FindFirstChild("CheckpointData")
			if checkpointData then
				local storedCheckpoint = storage.CheckpointData[userIdString].Value

				char:MoveTo(storedCheckpoint.Position + Vector3.new(math.random(-1, 2), 1, math.random(-1, 2)))

				humanoid:TakeDamage(30)	

			end
		end
		
		wait(cd)
		debounce = false
	end
end


script.Parent.Touched:connect(onTouch)

This is how I delete the checkpointData in the matchmaking system

local function clearCheckpointData()
	local checkpointFolder = game.ServerStorage:FindFirstChild("CheckpointData")
	
	if not checkpointFolder then return end
	
	if checkpointFolder then
		for _, instance in pairs(checkpointFolder:GetDescendants()) do
			if instance and instance:IsA("ObjectValue") then
				instance:Destroy()
			end
		end
	end
end

Pls help! I’m stuck here for like 3 days. Thanks in advance!

1 Like