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

Making players each spawn at a different spawn plate?

Asked by 6 years ago
Edited 6 years ago

When a player joins the game, he spawns at a spawn location and then gets teleported onto one of the spawn plates. The spawn plates have a bool value which state if a player has taken a spawn plate or not. Once a player takes it, it will set a value in their PlayerGui to true. This works pretty well but sometimes two or three players might be teleported to the same plate. Also, sometimes the player doesn't get teleported at all and stays at the original spawn location. There are no errors.

    local takenPlates = {} -- empty table containing used plates
    takenPlates[1] = 0
    takenPlates[2] = 0
    takenPlates[3] = 0
    takenPlates[4] = 0
    takenPlates[5] = 0
    takenPlates[6] = 0
    takenPlates[7] = 0
    takenPlates[8] = 0
    takenPlates[9] = 0
    takenPlates[10] = 0
    takenPlates[11] = 0
    takenPlates[12] = 0
    takenPlates[13] = 0
    takenPlates[14] = 0
    takenPlates[15] = 0
    takenPlates[16] = 0
    takenPlates[17] = 0
    takenPlates[18] = 0
    takenPlates[19] = 0
    takenPlates[20] = 0
-- What you would need to do is create enough plates to accomodate a full server, so if a server holds 20 maximim plateyers, create 20 plates.

local platesModel = workspace.spawnPlates
for _,plr in pairs(game.Players:GetPlayers()) do
    local num = math.random(1,20)
    local plate = platesModel["T"..num] --Pick random plate
    local p = game.Players.LocalPlayer.Character
    print(num)

    if num ~= takenPlates[num] then --Check that the selected plate is already taken
            --Code teleportation plr:MoveTo(game.Workspace.spawnPlates["T"..num].Part.Position)
            print("Entered 1")  
            if num == 1 then
                p:MoveTo(workspace.spawnPlates.T1.Part.Position)
            end
            if num == 2 then
                p:MoveTo(workspace.spawnPlates.T2.Part.Position)
            end
            if num == 3 then
                p:MoveTo(workspace.spawnPlates.T3.Part.Position)
            end
            if num == 4 then
                p:MoveTo(workspace.spawnPlates.T4.Part.Position)
            end
            if num == 5 then
                p:MoveTo(workspace.spawnPlates.T5.Part.Position)
            end
            if num == 6 then
                p:MoveTo(workspace.spawnPlates.T6.Part.Position)
            end
            if num == 7 then
                p:MoveTo(workspace.spawnPlates.T7.Part.Position)
            end
            if num == 8 then
                p:MoveTo(workspace.spawnPlates.T8.Part.Position)
            end
            if num == 9 then
                p:MoveTo(workspace.spawnPlates.T9.Part.Position)
            end
            if num == 10 then
                p:MoveTo(workspace.spawnPlates.T10.Part.Position)
            end
            if num == 11 then
                p:MoveTo(workspace.spawnPlates.T11.Part.Position)
            end
            if num == 12 then
                p:MoveTo(workspace.spawnPlates.T12.Part.Position)
            end
            if num == 13 then
                p:MoveTo(workspace.spawnPlates.T13.Part.Position)
            end
            if num == 14 then
                p:MoveTo(workspace.spawnPlates.T14.Part.Position)
            end
            if num == 15 then
                p:MoveTo(workspace.spawnPlates.T15.Part.Position)
            end
            if num == 16 then
                p:MoveTo(workspace.spawnPlates.T16.Part.Position)
            end
            if num == 17 then
                p:MoveTo(workspace.spawnPlates.T17.Part.Position)
            end
            if num == 18 then
                p:MoveTo(workspace.spawnPlates.T18.Part.Position)
            end
            if num == 19 then
                p:MoveTo(workspace.spawnPlates.T19.Part.Position)
            end
            if num == 20 then
                p:MoveTo(workspace.spawnPlates.T20.Part.Position)
            end
            takenPlates[num] = num --Insert taken plate into table
        else
            repeat num = math.random(1,20) plate = platesModel["T"..num] wait() until plate ~= num -- Keep trying to find an available plate until one has been found
            --plr:MoveTo(game.Workspace.spawnPlates["T"..num].Part.Position)           
            print("Entered 2")          
            if num == 1 then
                p:MoveTo(game.Workspace.spawnPlates.T1.Part.Position)
            end
            if num == 2 then
                p:MoveTo(game.Workspace.spawnPlates.T2.Part.Position)
            end
            if num == 3 then
                p:MoveTo(game.Workspace.spawnPlates.T3.Part.Position)
            end
            if num == 4 then
                p:MoveTo(game.Workspace.spawnPlates.T4.Part.Position)
            end
            if num == 5 then
                p:MoveTo(game.Workspace.spawnPlates.T5.Part.Position)
            end
            if num == 6 then
                p:MoveTo(game.Workspace.spawnPlates.T6.Part.Position)
            end
            if num == 7 then
                p:MoveTo(game.Workspace.spawnPlates.T7.Part.Position)
            end
            if num == 8 then
                p:MoveTo(game.Workspace.spawnPlates.T8.Part.Position)
            end
            if num == 9 then
                p:MoveTo(game.Workspace.spawnPlates.T9.Part.Position)
            end
            if num == 10 then
                p:MoveTo(game.Workspace.spawnPlates.T10.Part.Position)
            end
            if num == 11 then
                p:MoveTo(game.Workspace.spawnPlates.T11.Part.Position)
            end
            if num == 12 then
                p:MoveTo(game.Workspace.spawnPlates.T12.Part.Position)
            end
            if num == 13 then
                p:MoveTo(game.Workspace.spawnPlates.T13.Part.Position)
            end
            if num == 14 then
                p:MoveTo(game.Workspace.spawnPlates.T14.Part.Position)
            end
            if num == 15 then
                p:MoveTo(game.Workspace.spawnPlates.T15.Part.Position)
            end
            if num == 16 then
                p:MoveTo(game.Workspace.spawnPlates.T16.Part.Position)
            end
            if num == 17 then
                p:MoveTo(game.Workspace.spawnPlates.T17.Part.Position)
            end
            if num == 18 then
                p:MoveTo(game.Workspace.spawnPlates.T18.Part.Position)
            end
            if num == 19 then
                p:MoveTo(game.Workspace.spawnPlates.T19.Part.Position)
            end
            if num == 20 then
                p:MoveTo(game.Workspace.spawnPlates.T20.Part.Position)
            end

        end
    end

After a player is done customizing their character from the spawn plate and they hit spawn to teleport to the map, this code runs. It makes the boolean from their PlayerGui false.

game.Workspace:WaitForChild(game.Players.LocalPlayer.Name)
local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:connect(function()
    if script.Parent.Value.Value == 1 then
        workspace.SpawnTowers.T1.Taken.Value = false
    elseif script.Parent.Value.Value == 2 then
        workspace.SpawnTowers.T2.Taken.Value = false
    elseif script.Parent.Value.Value == 3 then
        workspace.SpawnTowers.T3.Taken.Value = false
    elseif script.Parent.Value.Value == 4 then
        workspace.SpawnTowers.T4.Taken.Value = false
    elseif script.Parent.Value.Value == 5 then
        workspace.SpawnTowers.T5.Taken.Value = false
    elseif script.Parent.Value.Value == 6 then
        workspace.SpawnTowers.T6.Taken.Value = false
    elseif script.Parent.Value.Value == 7 then
        workspace.SpawnTowers.T7.Taken.Value = false
    elseif script.Parent.Value.Value == 8 then
        workspace.SpawnTowers.T8.Taken.Value = false
    elseif script.Parent.Value.Value == 9 then
        workspace.SpawnTowers.T9.Taken.Value = false
    elseif script.Parent.Value.Value == 10 then
        workspace.SpawnTowers.T10.Taken.Value = false
    elseif script.Parent.Value.Value == 11 then
        workspace.SpawnTowers.T11.Taken.Value = false
    elseif script.Parent.Value.Value == 12 then
        workspace.SpawnTowers.T12.Taken.Value = false
    elseif script.Parent.Value.Value == 13 then
        workspace.SpawnTowers.T13.Taken.Value = false
    elseif script.Parent.Value.Value == 14 then
        workspace.SpawnTowers.T14.Taken.Value = false
    elseif script.Parent.Value.Value == 15 then
        workspace.SpawnTowers.T15.Taken.Value = false
    elseif script.Parent.Value.Value == 16 then
        workspace.SpawnTowers.T16.Taken.Value = false
    elseif script.Parent.Value.Value == 17 then
        workspace.SpawnTowers.T17.Taken.Value = false
    elseif script.Parent.Value.Value == 18 then
        workspace.SpawnTowers.T18.Taken.Value = false
    elseif script.Parent.Value.Value == 19 then
        workspace.SpawnTowers.T19.Taken.Value = false
    elseif script.Parent.Value.Value == 20 then
        workspace.SpawnTowers.T20.Taken.Value = false
    end

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Try assiging one spawn to each player.

-- Runs the function to assign a spawn to a player when they join

function assignSpawn(p)
    for i,v in pairs(game.Workspace.Spawns:GetChildren()) do
        if v.Name == "Unset" then
            v.Name = p.Name
        end
    end
end
game.Players.PlayerAdded:connect(assignSpawn)

-- Runs the restoration function for when the player leaves

game.Players.PlayerRemoving:connect(function(player)
    game.Workspace.Spawns:FindFirstChild(player.Name).Name = "Unset"
end)

That should go through every spawn point [stored in game.Workspace.Spawns, a folder, and named "Unset"] and finds one named Unset and claims it.

When the player leaves, this should rename the player's spawn to Unset, thus making it available again.

If this helped, set this as your accepted answer. For assistance, please contact me on Discord [profile]

Ad

Answer this question