How can I prevent math.random from choosing the same player

I’m working on a system and currently it’ll choose 2 players from a table generated by :GetPlayers from the players service, thing is that it might choose the same player, something that I obviously don’t want

Any ideas? CAuse I got none

5 Likes

Can you provide the script, so I can preview it?

1 Like

Hmm sure

be1.Event:Connect(function()
	while true do
		
		for count = 15, 1, -1 do
			
			re1:FireAllClients(count)
			wait(1)
		end
		
		p1 = players[math.random(1, #players)]
		
	end
end)

p1 is the first player, there’s gonna be p2 which is the second player, so my question is how can I prevent math.random from assigning the same player to p2

1 Like

use tables : Tables | Documentation - Roblox Creator Hub
Add all players to table. After player is selected, remove them from that table.

Nice

This text will be blurred

You can use a simple algorithm that shuffles the list of players and selects two players from the shuffled list boo, here’s how you can go about that:

--Get a table named players that will contain the players x

local function shuffleTable(tbl)
    local random = math.random
    local iterations = #tbl
    local j

    for i = iterations, 2, -1 do
        j = random(i)
        tbl[i], tbl[j] = tbl[j], tbl[i]
    end
end

shuffleTable(players)

local player1 = players[1]
local player2 = players[2]

while player2 == player1 do
    shuffleTable(players)
    player2 = players[2]
end

-- Now you have two different players selected xoxo
print(player1.Name)
print(player2.Name)
1 Like

Can you help me with this issue now?

Here’s the code

	while true do
		
		for count = 15, 1, -1 do
			
			re1:FireAllClients(count)
			wait(1)
		end
		
		shuffle(players)
		
		p1 = players[1]
		p2 = players[2]
		
		while p2 == p1 do
			shuffle(players)
			p2 = players[2]
			wait(0.1)
		end
		
		re2:FireAllClients(p1, p2)
		
	end

Watch the re2:FireAllClients

The localscript is:

re2.OnClientEvent:Connect(function(p1, p2)
	print("bruh")
	script.Parent.TextLabel.Text = "The Players have been chosen: "
end)

It's not running

The issue seems to be that the LocalScript is not running as expected when the re2:FireAllClients(p1, p2) event is fired, make sure the LocalScript is a child of an object that has the re2 RemoteEvent as its parent. Ensure that the LocalScript is properly placed in the hierarchy and is active and verify that the OnClientEvent event handler is properly connected to the re2 RemoteEvent. You can add some debug print statements to check if the connection is successful:

re2.OnClientEvent:Connect(function(p1, p2)
    print("Event received: p1 =", p1, "p2 =", p2)
    script.Parent.TextLabel.Text = "The Players have been chosen: "
end)

And you can check if the TextLabel object exists and is a child of the same object where the LocalScript is located. Ensure that the TextLabel’s name is spelled correctly. You can add a debug print statement to verify if the TextLabel object is being found:

print(script.Parent.TextLabel)

If the TextLabel object is not found or the name is incorrect, you need to update the code accordingly x

1 Like

Look I printed on both sides and both do not print, the for loop does run, idk if the player selection runs since I haven’t printed that

If the code in the LocalScript is not printing anything, it suggests that the event re2:FireAllClients(p1, p2) might not be reaching the clients or the LocalScript is not properly connected to the event

Yopu should make sure that the re2 RemoteEvent is properly defined and accessible, the LocalScript is correctly placed and enabled, the event connection is established correctly in the LocalScript, and the clients have the necessary permissions to run the LocalScript and receive the event. Additionally, consider using a different event, such as FireClient , to test if the LocalScript receives the event X

okThis text will be blurred