Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Issue with picking 2 random players?

So my pickrandom() function is having issues when finding player 2 I put in a repeat to make sure it doesn't pick the same player that has already been picked but it's not working and it's causing issues with teleporting so I'm pretty sure it's one of the variables causing problems..

if somebody could help me clean up the function and explain what went wrong I'd appreciate it.

line 50 - 65

local replicatedStorage = game.ReplicatedStorage
local status = replicatedStorage:WaitForChild('InfoValue')
local mapstorage = workspace.Mapstorage
local mapinfo = replicatedStorage:WaitForChild("Map")
local plr1 = replicatedStorage:WaitForChild("plr1")
local plr2 = replicatedStorage:WaitForChild("plr2")
local plr1died = replicatedStorage:WaitForChild("Player1Died")
local plr2died = replicatedStorage:WaitForChild("Player2Died")

local player1
local player2

_G. playing = {}

local intermissionTimer = 15

while true do
	while game.Players.NumPlayers < 2 do
		status.Value = "The game requires a minimum of 2 players please invite a friend or patiently wait.."
		repeat wait(2) until game.Players.NumPlayers >= 2
	end

	game.Players.Changed:connect(function()
		if game.Players.NumPlayers >= 2 then
			intermissionTimer = 30
		elseif game.Players.NumPlayers > 2 and game.Players.NumPlayers <= 8 then
			intermissionTimer = 20
		elseif game.Players.NumPlayers > 8 and game.Players.NumPlayers <= 15 then
		end
	end)
	for i = intermissionTimer,0,-1 do
		status.Value = "Intermission : "..i
		wait(1)
	end

	function giveplayer1skin()
			local sword = replicatedStorage.Sword
			local newsword = sword:Clone()
			newsword.Parent = game.Players:FindFirstChild(player1).Backpack
			game.Players:FindFirstChild(player1).leaderstats.Cash.Value = game.Players:FindFirstChild(player1).leaderstats.Cash.Value + 25
	end
	
	function giveplayer2skin()
			local sword = replicatedStorage.Sword
			local newsword = sword:Clone()
			newsword.Parent = game.Players:FindFirstChild(player2).Backpack
			game.Players:FindFirstChild(player2).leaderstats.Cash.Value = game.Players:FindFirstChild(player2).leaderstats.Cash.Value + 25
	end
		
	function pickrandom()
		local plrs = game.Players:GetPlayers()	
		local pl1 = plrs[math.random(1, #plrs)]
		player1 = pl1.Name
		print(player1)
		plr1.Value = player1
		
		local pl2
		
		repeat
			pl2 = plrs[math.random(1, #plrs)]
			player2 = pl2.Name
			plr2.Value = player1
			print(player2)
		until pl2 and pl2 ~= pl1
	end
	
	local mapinserverstorage = game:GetService('ServerStorage'):GetChildren()
	local chosenmap = mapinserverstorage[math.random(1, #mapinserverstorage)]
	local spawn1 = chosenmap:FindFirstChild("Spawns")['Spawn1']
	local spawn2 = chosenmap:FindFirstChild("Spawns")['Spawn2']
	
		mapinfo.Value = chosenmap.Name
		status.Value = "Loading Map : ".. chosenmap.Name
		chosenmap:Clone().Parent = mapstorage
		wait(5)
		status.Value = "Get ready to be teleported!"
		pickrandom()
		wait(1)
					plr1died.Value = false
					plr2died.Value = false
					wait(1)
					game.Players:FindFirstChild(player1).Character.Torso.CFrame = CFrame.new(spawn1.Position + Vector3.new(0,2,0))
					game.Players:FindFirstChild(player2).Character.Torso.CFrame = CFrame.new(spawn2.Position + Vector3.new(0,2,0))	
					wait(1)
					giveplayer1skin()
					giveplayer2skin()
					
		
		for i = 60, 0, -1 do
			if i == 0 then
				status.Value = "Time up!"
				-- kill players left in ring
				game.Players:FindFirstChild(player1).Character:BreakJoints()
				print('killing player1')
				game.Players:FindFirstChild(player2).Character:BreakJoints()
				print('killing player2')
				
				plr1died.Value = false
				plr2died.Value = false
				break
			end
			
			wait(1)
			if plr1died.Value == true then
						game.Players:FindFirstChild(player2):WaitForChild("leaderstats").Cash.Value = game.Players:FindFirstChild(player2):WaitForChild("leaderstats").Cash.Value + 250
						game.Players:FindFirstChild(player2)['leaderstats'].Wins.Value = game.Players:FindFirstChild(player2)['leaderstats'].Wins.Value + 1
						wait(2)
						game.Players:FindFirstChild(player2).Character:BreakJoints()
						print('killing winner')
						wait(1)
						status.Value = player2.. " Defeated ".. player1 -- victory text
						plr1.Value = ""
						plr1died.Value = false
						plr2.Value = ""
						player1 = ""
						player2 = ""
						break
			elseif plr2died.Value == true then
						game.Players:FindFirstChild(player1):WaitForChild("leaderstats").Cash.Value = game.Players:FindFirstChild(player1):WaitForChild("leaderstats").Cash.Value + 250
						game.Players:FindFirstChild(player1)['leaderstats'].Wins.Value = game.Players:FindFirstChild(player1)['leaderstats'].Wins.Value + 1
						wait(2)						
						game.Players:FindFirstChild(player1).Character:BreakJoints()
						print('killing winner')
						wait(1)
						status.Value = player1.. " Defeated ".. player2 -- victory text
						plr1.Value = ""
						plr2.Value = ""
						plr2died.Value = false
						player1 = ""
						player2 = ""
						break	
				end
				status.Value = i.. " seconds remaining!"
		end
	mapstorage:ClearAllChildren()
wait(5)
end

1 answer

✓ Accepted answer

In your pickrandom function, you wrote plr2.Value = player1 instead of plr2.Value = player2.

Here's a better method that doesn't use a repeat loop

function pickrandom()
	local players = game.Players:GetPlayers()
	player1 = table.remove(players, math.random(#players))
	player2 = players[math.random(#players)]
	plr1.Value = player1
	plr2.Value = player2
end

1 comment

0

Thank you :) I like your system a lot better I didn't think about coming at the function that way

This discussion is preserved as it was. New answers, comments, and votes are closed.