I have made it so i teleport a bunch of players to different locations, but sometimes they go to the same location. I want to assign every teleporting player a different location to teleport to all random. So if i have 8 players and 8 locations, every player has his own location. As if right now i can get random teleports but they can spawn on the same location. I have used the following code:
``
local lobbyLocation = game.Workspace.Lobby.Lobby.Position + Vector3.new(0,3,0) local ReplicatedStorage = game:GetService('ReplicatedStorage') local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent') local function playGame() local timeAmount = 20 local timerText = 'Remaining Time: ' while timeAmount > 0 do timeEvent:FireAllClients(timeAmount, timerText) wait(1) timeAmount -= 1 end end local function playIntermission() local intermission = 10 local timerText = 'Intermission: ' while intermission > 0 do timeEvent:FireAllClients(intermission, timerText) wait(1) intermission -= 1 end end local function resetPlayers() for _, plr in pairs(game.Players:GetChildren()) do plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation) end end local function teleportPlayers() local plrs = game.Players:GetChildren() for i = 1, #plrs do local num = math.random(1,9) plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Main"..num].Position) end end while true do resetPlayers() playIntermission() teleportPlayers() playGame() end
Ok, so the problem is that when you get a random value using math.random() it sometimes can repeat the same numbers.
So I made a script that allows you to get a random value without it repeating itself
Basically, it gets a random number, check if it exists in a table and if not it will return it and return true and put it in the table and if it exists in the table it will return nil and return false. With this you can use the repeat to loop it until it gets true.
local function GetRandomValue(min, max, tableList) local number = math.random(min,max) local success = true for i, v in ipairs(tableList) do if v == number then success = false end end if success then table.insert(tableList, number) return number, true elseif not success then return nil, false end end local function teleportPlayers() local plrs = game.Players:GetChildren() local value = {} for i = 1, #plrs do local num, success repeat num, success = GetRandomValue(1, 9, value) until success plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Main"..num].Position) end end
I hope this helps.
local function TeleportPlayers() local PlayersToTeleport = Players:GetPlayers() local Teleports = Teleports:GetChildren() --------------- if (#PlayersToTeleport <= #Teleports) then --------------- for _, Player in ipairs(PlayersToTeleport) do --------------- local Teleport = table.remove(Teleports, math.random(#Teleports)) --------------- if (Player.Character) then --------------- local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid") if (Humanoid and Humanoid.Health > 0) then --------------- Humanoid.RootPart.CFrame = CFrame.new( Teleport.Position + Vector3.new(0, 3.5, 0) ) end end end else error( "Player-teleport ratio unbalanced; not enough teleports to evenly distribute." ) end end