LuluPls
|
  |
| Joined: 23 Aug 2010 |
| Total Posts: 1221 |
|
|
| 13 Feb 2017 09:26 AM |
I'm trying to teleport all players to non-occupied spawns
The spawns are located in game.Workspace.First.FirstLevelSpawns and each have a boolvalue caled "Occupied" inside it. For each player I want to check if the part is occupied. If it is then I want to repick, if it isn't I want it to change the value of Occupied to true and teleport them to that respective part.
This is my function so far, any help is appreciated:
function Startgame() --Teleporting all players to spawns tab={} for i,v in pairs(game.Workspace:WaitForChild("First"):WaitForChild("FirstLevelSpawns"):GetChildren()) do table.insert(tab,v) end for i,v in pairs(game.Players:GetChildren()) do for i,c in pairs(tab) do if c.Occupied.Value==false then v.Character:MoveTo(c.Position) --else end end end end |
|
|
| Report Abuse |
|
|
LuluPls
|
  |
| Joined: 23 Aug 2010 |
| Total Posts: 1221 |
|
|
| 13 Feb 2017 09:27 AM |
| I'm aware I have a few extra ends on there, this isn't the entire function. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 13 Feb 2017 09:35 AM |
local spawns = workspace.First.FirstLevelSpawns:GetChildren()
for i,v in next, game.Players:GetPlayers() do v.Character:MoveTo(spawns[i].Position) end
|
|
|
| Report Abuse |
|
|
|
| 13 Feb 2017 09:49 AM |
| not smart cuz u can never guarantee how many spawns there r in comparison to players |
|
|
| Report Abuse |
|
|
Xonae
|
  |
| Joined: 04 Dec 2013 |
| Total Posts: 336 |
|
|
| 13 Feb 2017 10:22 AM |
Make sure you have enough spawns for each player, if each player is teleported to a unique spawn.
function FindSpawn() local spawns=game.Workspace.First.FirstLevelSpawns:GetChildren() local newspawn while true do local spawn=spawns[math.random(1,#spawns)] if spawn.Occupied.Value==false then spawn.Occupied.Value=true -- feel free to remove this and replace it somewhere else newspawn=spawn break end end
return newspawn end
this function will find a random spawn that isn't occupied, this function will however crash your game if there are no longer any spawns that can be occupied.
~ Xonae ~ |
|
|
| Report Abuse |
|
|
LuluPls
|
  |
| Joined: 23 Aug 2010 |
| Total Posts: 1221 |
|
|
| 15 Feb 2017 06:59 AM |
| Thanks TimeTicks. I don't know how I didn't think of that. |
|
|
| Report Abuse |
|
|
|
| 15 Feb 2017 07:09 AM |
| coolrainbow, don't try helping if you have no idea what is going on |
|
|
| Report Abuse |
|
|
OzzyFin
|
  |
| Joined: 07 Jun 2011 |
| Total Posts: 3600 |
|
|
| 15 Feb 2017 07:28 AM |
If you want to randomize the spawns a little bit:
local players = game.Players:GetPlayers() table.sort(players,function(x,y) return math.random(2) == 1 end)
then iterate through 'players'
|
|
|
| Report Abuse |
|
|
|
| 17 Feb 2017 09:53 AM |
@half
no, you're just too idiotic to understand the approach kid |
|
|
| Report Abuse |
|
|
LuluPls
|
  |
| Joined: 23 Aug 2010 |
| Total Posts: 1221 |
|
|
| 17 Feb 2017 02:14 PM |
Half is right though, you're stupid.
|
|
|
| Report Abuse |
|
|
|
| 18 Feb 2017 12:30 AM |
omg you're both so dull
it doesnt check if you have enough spawns ffs |
|
|
| Report Abuse |
|
|
npott13
|
  |
| Joined: 19 Dec 2010 |
| Total Posts: 264 |
|
|
| 18 Feb 2017 02:59 AM |
coolrainbow, please tell me ur joking...
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Feb 2017 04:02 AM |
coolrainbow is the only smart one here. You guys are seriously stupid if you don't understand what he's saying. |
|
|
| Report Abuse |
|
|
|
| 18 Feb 2017 04:07 AM |
| local spawns = workspace.First.FirstLevelSpawns:GetChildren() for i, # in pairs(game.Players:GetPlayers()) do if ########### ## nil then ############################################################ end end will spawn characters in each spawn, then when spawns run out spawn them from first spawn again |
|
|
| Report Abuse |
|
|
|
| 18 Feb 2017 04:09 AM |
| local spawns = workspace.First.FirstLevelSpawns:GetChildren() for i, player in pairs(game.Players:GetPlayers()) do if player.Character ## nil then player.Character:MoveTo(spawn[math.max(1,i % #################### end end |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Feb 2017 04:10 AM |
... v.Character:MoveTo(spawns[i%#spawns+1].Position)
@Ozzy that's a very bad way to randomize a table. There's a simpler and more efficient way: for idx = 1, #tb/2 do local idx2 = math.random(len) tb[idx], tb[idx2] = tb[idx2], tb[idx] end |
|
|
| Report Abuse |
|
|
|
| 18 Feb 2017 04:11 AM |
thats what i was trying to type
you have let me down preview
|
|
|
| Report Abuse |
|
|
OzzyFin
|
  |
| Joined: 07 Jun 2011 |
| Total Posts: 3600 |
|
|
| 18 Feb 2017 05:19 AM |
yeah I totally forgot about how sorting algorithms work
It'd possibly get into an infinite loop if lua didn't error before that, right? |
|
|
| Report Abuse |
|
|
OzzyFin
|
  |
| Joined: 07 Jun 2011 |
| Total Posts: 3600 |
|
|
| 18 Feb 2017 05:22 AM |
well not infinite, the odds of math.random generating such a pattern aren't too good for that more like a long loop that'd take way longer than it should to do the task |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|