Spawn Location Problems

So recently I added a 2nd spawn location in my game.

Its sort of an obby game you could say.

So I want the player to spawn in one place and one place only but instead it randomly spawns between the 2.

The 2nd thing is that if the player hits spawn 2 and dies It sometimes goes to spawn 1 and vice versa.

How could I fix this?

(Tell me if I should change the category. I didn’t know where to put it.)

1 Like

You need to go to the Players service and choose a player. Set their RespawnLocation to the spawn location where you want to make them respawn.

1 Like

So would I do that with a script?

I don’t know if you want a player to touch the checkpoint, so you can just do it with a Touched event.

Part.Touched:Connect(function(hit) --
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    
    if player then
        player.RespawnLocation = Part
    end
end)

Make sure the class name of the part is RespawnLocation.

2 Likes

Would this be a local script underneath the RespawnLocation?

You can handle it through ServerScriptService since the property should be changed through the server.

1 Like

So I would have to name everyone in my game then?

(I mean every spwnlocation)

Maybe, you can rename each checkpoint’s name to their stage number. But this time, you would use for loop. It would be easier than the previous one.

for i, v in ipairs(workspace.CheckpointFolder:GetChildren()) do
    v.Touched:Connect(function(hit)
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if tonumber(v.Name) == (player.leaderstats.Stage.Value + 1) then

            if player then
                player.leaderstats.Stage.Value = tonumber(v.Name)
                player.RespawnLocation = v
            end
        end
    end)
end

I just gave an example of how you can increase the value & specify a new respawn location. You can change the name --CheckpointFolder, leaderstats or stage- whatever you want to.

@MasterObstacles Thanks, I just noticed that I wrote them wrong.

2 Likes

Try making teams and with each team is a spawn location. When the player touches the spawn location, they automatically change teams. When this happens, every time they die, they will respawn at the spawn location they are required to spawn at.

I believe you can also save the team/team color they are at with datastore, but I have never tried.

In the properties of SpawnLocation, disable Enabled in which you do not want them to be generated.

1 Like

Just make sure the player variable is above the if statement or you will get the error “attempt to index nil with leaderstats”

for i, v in ipairs(workspace.CheckpointFolder:GetChildren()) do
    v.Touched:Connect(function(hit)
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if tonumber(v.Name) == (player.leaderstats.Stage.Value + 1) then

            if player then
                player.leaderstats.Stage.Value = tonumber(v.Name)
                player.RespawnLocation = v
            end
        end
    end)
end
2 Likes