Checkpoint Script not working in multiplayer

Hey everyone,

I’m currently running into a bit of a problem a game I’m working on, specificly my checkpoint system. Throughout my game I have checkpoints that work similarly to bonfires from darksouls, you touch them and the next time you die you’re transported back to them. A problem I ran into while testing the game with multiple people is that the checkpoints only work for one player on the server and not everyone for some reason and I’m completely clueless as to why that is and would appreciate any suggestions. Below is the script in question

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

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
			
			player.CharacterAdded:connect(function(character)
				wait()
				local storedCheckpoint = ServerStorage.CheckpointData[userIdString].Value
				character:MoveTo(storedCheckpoint.Position + Vector3.new(math.random(-4, 4), 4, math.random(-4, 4)))
			end)
		end
		
		checkpointValue.Value = checkpoint
	end
end

checkpoint.Touched:Connect(onTouched)
1 Like