It is going to be used in a dueling game, only two players will be fighting at one given time. I will make it wait for the player’s deaths.
I see, well for the sake of testing maybe just add a wait(10) on the top, but when you actually use it in the game put it in a function and call that function whenever you are selecting two players to fight.
I still received 0 player output, I guess I will have to attach the if function onto when someone joins the game, but what if noone is joining the game and I need another match to start, maybe a timer or some sort?
I usually have a gui on the top of the screen that says:
Waiting for More Players (2)…
then:
repeat wait() until #game.Players > 1
Yeah, but I’m saying in terms of scripting that - should I just have a 10 second timer just checking for 2 or more players and checking that there isn’t an active duel then execute the if statement?
You could make the script wait for the desired amount of players before continuing:
repeat wait() until #game.Players:GetPlayers() >= 2
Right before the if statement do:
repeat wait() until #game.Players >1
it will check multiple times a second to see if the number of players is greater than 1. If it is it will move on, if it isn’t it will keep on checking.
I’m still having issues with removing a random player from the Participants table and saving their name so I can then teleport them to a certain pad.
I feel as if this is redundant and wrong execution, but hey.
repeat wait(2) until #Players:GetPlayers() >= 1
if #Players:GetChildren() >= 1 then
print(#Participants)
local Player1 = table.remove(Participants, math.random(#Participants))
print(Player1)
end
WAIT - I think participants is empty?
I forgot about… Players.PlayerAdded:Connect(onPlayerAdded) needs to be infront.
You need to do #Players:GetChildren() instead of Players:GetChildren() to get the length of the players table.
local playersToTeleport = {}
local function teleportRandomPlayer()
local n = math.random(1,#playersToTeleport)
local chosenPlayer = playersToTeleport[n]
table.remove(playersToTeleport, n)
-- and then use chosenPlayer when doing the teleporting.
end
It’s best to use :GetPlayers() instead of :GetChildren() in the event that a non-player object gets parented to the players service.
We’ve got it finished, it mainly was presenting problems with the fact I had to include my Players.PlayerAdded:Connect(onPlayerAdded)
before the repeat wait()
because otherwise Participants would be an empty table.
It works for what I wanted and now I can get to figuring out the teleportation assigning of swords and timer for the match.