I’m trying to make a game where one player is a sniper, and the rest are ducks who have to complete an obstacle course to escape the sniper, but i cant find any good info on how to put them on separate teams randomly.
I am not quite sure how you’ve implemented teams, but it’s as simple as this:
local Players = game:GetService("Players")
local playerInstances = Players:GetPlayers() -- List of players
local sniperIndex = math.random(1, #playerInstances) -- A psuedorandom number between 1 and the amount of players.
local sniperPlayer = playerInstances[sniperIndex]
-- team player to sniper
for i, player in ipairs(playerInstances) do
if i ~= sniperIndex then -- If the index is not the sniper...
-- team player to duck
end
end
You would need to adapt this to your own system. If you have any further questions, feel free to ask. Hope this helped.
The system for the teams isn’t anything crazy, it’s just the default teams placed in the teams folder.
Perfect! Then, when you need to team players, you can use the post above. Implement your teaming logic where necessary. If this answered your question, I’d appreciate if you marked as solution. Let me know if you need anything else- have a great day.
I’ve been trying a few things to use the script, but it spits out this error every time.
ServerScriptService.team:5: invalid argument #2 to ‘random’ (interval is empty)
ive also tried adding a task.wait in order to wait for the players to join the game (server and clients).
Can you share the code that you are trying to use?
local Players = game:GetService(“Players”)
local playerInstances = Players:GetPlayers() – List of players
local sniperIndex = math.random(1, #playerInstances) – A psuedorandom number between 1 and the amount of players.
local sniperPlayer = playerInstances[sniperIndex]
task.wait(10)
for i, player in ipairs(playerInstances) do
if i ~= sniperIndex then – If the index is not the sniper…
– team player to duck
player.Team = game.Teams.Duck
else
player.Team = game.Teams.Sniper
end
end
Someting if you call this function should work quickly.
local plrs = game:GetService("Players")
local function doTeamSwitch()
local playerThatIsTheLuckyOne = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]
for _,v in plrs:GetChildren() do
if v:IsA("Player") then
if v==playerThatIsTheLuckyOne then
v.Team = game.Teams.onePlayerTeam -- sniper or something
else
v.Team = game.Teams.otherPlayersTeams -- players or something
end
end
end
end
doTeamSwitch()
By the way, if there’s only one player then it’s just always picking that one player.
edit: First, it gets all players in the game. and the after that it makes a variable and the variable is always a random player in the game. It then loops thru the players, and sees if the looped player is the sniper or not. If so, then we set the team as that player to Sniper. Else, it goes to the normal player team.
This error is occurring because the task.wait is misplaced.
This is a poor approach.
You should wrap this functionality in a function so you can trigger it when necessary.
To patch it quickly, however, you can do the following:
local playerInstances = {}
while #playerInstances < 1 do
playerInstances = Players:GetPlayers()
wait(.1)
end
The error occurs because the max argument of math.random is less than the minimum.
The script works, thanks alot for the help!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.