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

Respawning in round system?

Asked by 3 years ago
Edited 3 years ago

So, I have this round script and it works fine, but I want the player to respawn at one of the spawns when dead, instead of respawning in the lobby.

The round system goes like this...

  • Intermission countdown begins

  • A map or a specific map is chosen

  • Players are teleported to a spawn by their "HumanoidRootParts". The spawn that was picked for that player is then removed from a table so it doesn't get called again.

The problem here is that I'm not sure as to how they would respawn at one of spawns when dead. I've tried doing this by using the same lines of code which teleports them to the spawns, but rather this happens repetitively and the player respawns at nearly all the spawns. This shouldn't be happening, and should only happen once. After around 10 seconds, I get an error in the output which states "ServerScriptService.RoundScript: 132: attempt to index nil with CFrame". Why is this happening? Is this because both the spawn functions are interfering? Additionally, it seems to be repetitive almost in like a loop, because my output keeps printing the relative print that fallows this spawning function, e.g "has been removed(*10). Is there any more efficient way to do this, I feel like I'm missing something here. I've been stuck on this for sometime and have not found any way to solve it. This is my first post and any help would greatly appreciable. Thankyou!

This is the round script,

    -- Define Variables

    local ReplicatedStorage = game:GetService("ReplicatedStorage")

    local ServerStorage = game:GetService("ServerStorage")

    local MapsFolder = ServerStorage:WaitForChild("Maps")

    local Status = ReplicatedStorage:WaitForChild("Status")

    local GameLength = 240

    local cashreward = 25

    local gemsreward = 5

    local trophiesreward = 25

    local TweenService = game:GetService("TweenService")

    local statustextlabel = game.StarterGui.Status.TextLabel

    -- Game Loop

    while true do

        Status.Value = "Waiting for enough Players"

        repeat wait(1) until #game:GetService('Players'):GetPlayers() >= 1

        Status.Value = "Intermission"

        wait(10)

        local plrs = {}

        for i, player in pairs(game.Players:GetPlayers())do
            if player then
            table.insert(plrs,player) -- Add each player into plrs table
       end
    end

    wait(2)

    local AvailableMaps = MapsFolder:GetChildren()

    local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]

    Status.Value = " Teleporting to "..ChosenMap.Name..""

    local ClonedMap = ChosenMap:Clone()
    ClonedMap.Parent = workspace
    wait(2)

    -- Teleport players to the map

    local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

    if not SpawnPoints then
        print("Spawnpoints not found!")
    end

    local AvailableSpawnPoints = SpawnPoints:GetChildren()

    for i, player in pairs(plrs)do
        if player then
            character = player.Character

            if character then
                -- Teleport them

                character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,5,0)
                 table.remove(AvailableSpawnPoints,1)

                -- Give them a sword 
                local equipped = game.ServerStorage.PlayerData[player.Name].Equipped

                if equipped.Value ~= "" then
                    local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
                    weapon.Parent = player.Backpack
                else
                    local Sword = ServerStorage.Sword:Clone()
                    Sword.Parent = player.Backpack
                end

                local equipped2 = game.ServerStorage.PlayerData[player.Name].Equipped2

                if equipped2.Value ~= "" then
                    local weapon2 = game.ServerStorage.Items[equipped2.Value]:Clone()
                    weapon2.Parent = player.Backpack
                else
                    local Gun1 = ServerStorage.Gun1:Clone()
                    Gun1.Parent = player.Backpack
                end

                local GameTag = Instance.new("BoolValue")
                GameTag.Name = "GameTag"
                GameTag.Parent = player.Character

            else
                -- There is no character
                if not player then
                    table.remove(plrs,i)
    end
    end
    end
    end


    Status.Value = "Get ready to play!"
    wait(2)

    Status.Value = "Be the last one standing, you only have one chance!"
    wait(2)

    for i = GameLength,0,-1 do

    for x, player in pairs(plrs)do
    if player then

        character = player.Character

        if not character then
         -- Left the game
        table.remove(plrs,x)
     else
     if character:FindFirstChild("GameTag")then
         -- They are still alive
         print(player.Name.." is still in the game!")
    else
        -- They are dead                
        character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[2].CFrame + Vector3.new(0,5,0)
        table.remove(AvailableSpawnPoints,2)
        print(player.Name.." has been removed!")    
    end 
    end                                                             
    else                                
        print(player.Name.." has been removed!")    
    end
    end             

        Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left"


        if plrs[1].leaderstats.Kills.Value >= 30 then
        -- Last person standing    
        Status.Value = "The winner is "..plrs[1].Name
        plrs[1].leaderstats.Coins.Value = plrs[1].leaderstats.Coins.Value + cashreward
        plrs[1].leaderstats.Gems.Value = plrs[1].leaderstats.Gems.Value + gemsreward 
        plrs[1].leaderstats.Trophies.Value = plrs[1].leaderstats.Trophies.Value + trophiesreward

        break
     elseif #plrs == 0 then
        Status.Value = "Nobody won!"
        break
     elseif i == 0 then
        Status.Value = "Time up!"
        break   
        end 

        wait(1)
    end

    print("End of game")

    wait(2)

    for i, player in pairs(game.Players:GetPlayers()) do
        character = player.Character

        if not character then
        -- Ignore them
        else

             if character:FindFirstChild("GameTag") then
                 character.GameTag:Destroy()
            end

            for _, tool in pairs(player.Backpack:GetChildren()) do
                if tool:FindFirstChild("Price") then
                    tool:Destroy()
                end
            end

            for _, tool in pairs(character:GetChildren()) do
                if tool:FindFirstChild("Price") then
                    tool:Destroy()
                end
            end

        end

        player:LoadCharacter()
    end

    ClonedMap:Destroy()

    Status.Value = "Game ended"  

    wait(2)
end

Answer this question