local checkpointFolder = workspace:WaitForChild("Fake Checkpoints")
local checkpoint = checkpointFolder:WaitForChild("Checkpoint1")
_G.runbefore = false
checkpoint.Touched:Connect(function(hit)
if _G.runbefore == false then
local partParent = hit.Parent
local player = Players.LocalPlayer
local humanoid = partParent:FindFirstChild("Humanoid")
local realSpawnLocation = workspace:WaitForChild("RealSpawnLocation")
if humanoid then
realSpawnLocation.Position = Vector3.new(100,100,100)
realSpawnLocation.Anchored = true
player.RespawnLocation = realSpawnLocation
_G.runbefore = true
end
end
end)
local Players = game:GetService("Players")
local function addSpawn(spawnLocation)
-- listen for the spawn being touched
spawnLocation.Touched:Connect(function(hit)
local character = hit:FindFirstAncestorOfClass("Model")
if character then
local player = Players:GetPlayerFromCharacter(character)
if player and player.RespawnLocation ~= spawnLocation then
local humanoid = character:FindFirstChildOfClass("Humanoid")
-- make sure the character isn't dead
if humanoid and humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
print("spawn set")
player.RespawnLocation = spawnLocation
end
end
end
end)
end
local firstSpawn
-- look through the workspace for spawns
for _, descendant in pairs(workspace:GetDescendants()) do
if descendant:IsA("SpawnLocation") then
if descendant.Name == "FirstSpawn" then
firstSpawn = descendant
end
addSpawn(descendant)
end
end
local function playerAdded(player)
player.RespawnLocation = firstSpawn
end
-- listen for new players
Players.PlayerAdded:Connect(playerAdded)
-- go through existing players
for _, player in pairs(Players:GetPlayers()) do
playerAdded(player)
end
Looks similar to what you are trying to do, though they are using touch event on the spawnlocation itself.
It prints spawnLocation as Players.Magpies303
The local script is
local checkpointFolder = workspace:WaitForChild("Fake Checkpoints")
local checkpoint = checkpointFolder:WaitForChild("Checkpoint1")
_G.runbefore = false
checkpoint.Touched:Connect(function(hit)
if _G.runbefore == false then
local partParent = hit.Parent
local player = Players.LocalPlayer
local humanoid = partParent:FindFirstChild("Humanoid")
--local newSpawnLocation = workspace:WaitForChild("RealSpawnLocation")
workspace:WaitForChild("RealSpawnLocation"):Destroy()
local newSpawnLocation = Instance.new("SpawnLocation")
newSpawnLocation.Name = "RealSpawnLocation"
newSpawnLocation.Parent = workspace
if humanoid then
newSpawnLocation.Position = Vector3.new(100,100,100)
newSpawnLocation.Anchored = true
game.ReplicatedStorage.UpdateCheckpoint:FireServer(player, newSpawnLocation)
--player.RespawnLocation = newSpawnLocation
_G.runbefore = true
end
end
end)
FireServer doesn’t need player as first arg. That is included automatically. The handler is prob receiving {player, player, newSpawnLocation} and grabs the first two.
Instead of just moving spawn… I recommend just make a folder of spawn with number name. When player reaches that spawn point. A number value will get that spawnpoint’s name.
local Spawn = workspace.(FileName):FindFirstChild(NumberValue)
if Spawn then
game.Players.LocalPlayer.Character.PrimaryPart.CFrame = Spawn.CFrame + Vector3.new(0,6,0)
Thank you everyone!
I have solved the problem, the solution was to set all of the checkpoints as spawnlocations and then changing the spawnlocation from the server side to one of those checkpoints, I needed to make sure the checkpoints were replicated on both the server and client side which means I created them beforehand inside of the workspace.