Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with spawning in the correct sequence?

Asked by
waifuSZN 123
7 years ago

I have 15 spawns in my game, each named "1" through "15", respectively, stored in a Folders object named SpawnLocations.

This is the corresponding code:

local REQUIRED_PLAYERS = 1

game.Players.PlayerAdded:Connect(function(player)

for n, player in pairs(game.Players:GetPlayers()) do
    local playerTable = {}
    local n = 1

    playerTable[n] = player
    n = n + 1
    end 

if game.Players.NumPlayers == REQUIRED_PLAYERS then
    for n = 1, 1 do
        player:LoadCharacter()
        player.Character.PrimaryPart.Position = game.Workspace.SpawnLocations[tostring(n)].Position
    end
end

end)

The problem that arises, is that when I go to test this via server, the player joins, but spawns at spawn #6, instead of spawn #1. Also, his head falls off.

I am unsure where the code in incorrect, but I'm willing to hear all suggestions.

1 answer

Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
7 years ago

You didn't describe exactly what the code is supposed to do, so I made a guess:

local Players = game:GetService("Players")
local SpawnLocations = workspace.SpawnLocations
local i = 1

Players.CharacterAutoLoads = false

Players.PlayerAdded:Connect(function(Player)
    Player.RespawnLocation = SpawnLocations[i] -- Assign new Player to a Spawn
    i = i + 1
    Player:LoadCharacter()
end)
0
Thank you for this answer. Works as I wanted it to. I must have overthought the spawning process, you made it look so simple. Thanks again. waifuSZN 123 — 7y
0
Yup! No problem. Roblox has a lot of things built-in to make life easier! And for things that aren't built-in, I've written some modules for those. Let me know if you need any more help! Add me on Discord: Niles#8328 Validark 1580 — 7y
0
You also might want to make an adaptive system that accounts for when players leave the game, and a new one takes their place. The simple version I wrote for you only accounts for players entering and staying in the game. Send me your finalized code when you've completed it! Validark 1580 — 7y
Ad

Answer this question