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

I need to fix this error in my Team Death Match game. Help?

Asked by
bx3t 2
4 years ago
--So I'm basically gonna just explain my script right now and what's wrong with it.

--So there is nothing wrong with the script but one thing.

--Since this is a TDM game I need to divide the players on to the teams but that's just it.

--It puts random players on random teams.

--So lets say I have 2 players in the game and the round starts, They could possibly be on the same team which isn't really good.

--I hope you guys understand here is the script:

local weapon = game.ReplicatedStorage:WaitForChild("Sword") --The main weapon that gets
                                                                                                         --given to everyone later on                                                    
local matchIsInProgress = false --If the match is not  in progress
local Raiders = game.Teams.Raiders:GetPlayers() --Team 1
local Knights = game.Teams.Knights:GetPlayers() --Team 2
                                                                                      --Also there is lobby that is the default 
                                                                                            --team.
function spawnTeam(team) --Spawn all players to there starting points (depends on there         
                                            --team

    for _, player in pairs(team:GetPlayers())do
        local teamSpawns = game.Workspace[team.Name]:GetChildren()
        local randomSpawn =  teamSpawns[math.random(1,#teamSpawns)] 

        player.Character.HumanoidRootPart.CFrame = randomSpawn.CFrame
    end
end

function givePlayersRandomTeams() --This is my error that I explained up there. ^^^^^^
    local players = game.Players:GetChildren() --Gets all players

    for _, player in pairs(game.Players:GetPlayers())do
        local teamsTable = {game.Teams.Knights, game.Teams.Raiders}  --The TDM teams.
        local randomTeam = teamsTable[math.random(1,#teamsTable)] --Chooses random team

        player.Team = randomTeam --Changes team.
    end
end

function givePlayersWeapons() --Give players the weapon
    for _, player in pairs(game.Players:GetPlayers())do --Getting all players
        local clone = weapon:Clone() --Cloning the weapon.
        clone.Parent = player.Backpack --Making the Parent the players backpacks
    end
end

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

    player.CharacterAdded:Connect(function(character) --On player died
        character.Humanoid.Died:Connect(function()
            print("player died") 
            player.Team = game.Teams.Lobby --Setting the player to its default team.


        end)
    end)

    --start the match if there are enough players and the game isn't in progress
    if((#game.Players:GetPlayers()) >= 2 and not matchIsInProgress)then --If there are atleast
        print("there are enough players, start the match")                                       --2 players
                                                                                                                    --Then it would start

        wait(10) --wait 10 seconds for intermission

        matchIsInProgress = true --The game IS in progress.
        givePlayersRandomTeams() --Gives random team (error)
        givePlayersWeapons() --gives all players weapons
        spawnTeam(game.Teams.Knights) --Spawns the Knights
        spawnTeam(game.Teams.Raiders) --Spawns the Raiders
    end
end)



Hope you understand I hope to here from you guys soon!

0
Um is it fine if you shorten your script i dont know where the error is 123nabilben123 499 — 4y

Answer this question