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)
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)
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)