Checkpoint script spawning at other players' checkpoint

So my checkpoint script is being really strange. When I played the game solo without other players, I spawn at my correct checkpoint. But when I join a server where there are other players in it, I spawn at the checkpoint of the player that most recently joined the server. I do spawn at my correct checkpoint if I reset. But I want it to spawn correctly the minute I join the game, not after resetting. I am confused on what is causing this issue. For context, this is a server script!

local DataRemoteEvents = game.ReplicatedStorage:FindFirstChild("DataRemoteEvents")
local dataLoaded = DataRemoteEvents.dataLoaded

--player not spawning at proper checkpoint. Possibly has to do with playeradded event.
game.Players.PlayerAdded:Connect(function(plr)
	--task.wait(1)
	local leaderstats = plr:WaitForChild("leaderstats")
	local Stages = leaderstats:FindFirstChild("Stages")
	
	local function Spawning(character) -- handle spawning functionalities.
		print("Spawning...")
		local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
		HumanoidRootPart.CFrame = Checkpoints[tostring(Stages.Value)].CFrame * CFrame.new(0, 5, 0)
	end
	plr.CharacterAdded:Connect(Spawning)
	dataLoaded.Event:Connect(Spawning)
	
end)

Try moving these functions into a seperate script and see if it works, I’ve had a similar case of this before.

How would I move those functions into a separate script? They won’t work if I do that.

local DataRemoteEvents = game.ReplicatedStorage:FindFirstChild("DataRemoteEvents")
local dataLoaded = DataRemoteEvents.dataLoaded
game.Players.PlayerAdded:Connect(function(plr)
    local function Spawning(character) -- handle spawning functionalities.
        local leaderstats = plr:WaitForChild("leaderstats")
	    local Stages = leaderstats:FindFirstChild("Stages")
		print("Spawning...")
		local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
		HumanoidRootPart.CFrame = Checkpoints[tostring(Stages.Value)].CFrame * CFrame.new(0, 5, 0)
	end
     plr.CharacterAdded:Connect(Spawning)
     dataLoaded.Event:Connect(Spawning)
end)

I believe this could work

This doesn’t work because Stages.Value is nil. We need to be able to access Stages because that is what will tell us which checkpoint to spawn on…

Apologies, I forgot to declare the variables before, I edited the script in my previous response, let me know if it works

Still doesn’t work. I am spawning in but at the wrong checkpoint like before.

local DataRemoteEvents = game.ReplicatedStorage:FindFirstChild("DataRemoteEvents")
local dataLoaded = DataRemoteEvents.dataLoaded
game.Players.PlayerAdded:Connect(function(plr)
    local function Spawning(character) -- handle spawning functionalities.
        local leaderstats = plr:WaitForChild("leaderstats")
	    local Stages = leaderstats:FindFirstChild("Stages")
		print("Spawning...")
		local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
		HumanoidRootPart.CFrame = Checkpoints[tostring(Stages.Value)].CFrame * CFrame.new(0, 5, 0)
	end
     dataLoaded.Event:Connect(Spawning)
     task.wait(5)
     plr.CharacterAdded:Connect(Spawning)
end)

Okay I was able to fix it. I had to do two things. First, I moved the lines of code into the same script as as the script that loaded data from my datastores since that used a playerAdded event as well. I also had to a line a code “Spawning(plr.Character)”. Thanks for your help!

game.Players.PlayerAdded:Connect(function(plr)
	local character = workspace:WaitForChild(plr.Name)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Stages = Instance.new("IntValue")
	Stages.Name = "Stages"
	Stages.Parent = leaderstats
	
	local success, getStage = pcall(function()
		return stageData:GetAsync(plr.UserId)
	end)
	if success then
		if getStage == nil then
			Stages.Value = 1
		else
			Stages.Value = getStage
		end
	else
		Stages.Value = 1
	end
	
	local function Spawning(character) -- handle spawning functionalities.
		print("Spawning...")
		local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
		if HumanoidRootPart == nil then return end
		HumanoidRootPart.CFrame = Checkpoints[tostring(Stages.Value)].CFrame * CFrame.new(0, 5, 0)
	end
	plr.CharacterAdded:Connect(Spawning)
	task.wait(1)
	Spawning(plr.Character)
	dataLoaded.Event:Connect(Spawning)
	
	dataLoaded:Fire(plr) -- fire event when data finish loading.
end)
1 Like