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

Unable to print after a repeat loop?

Hi there. So I'm wondering why I cannot print a variable after it has been in a repeat loop. Take a look at this script here:

wait(20)

local playerlogger = {}

for i, player in pairs(game.Players:GetPlayers()) do
	table.insert(playerlogger, player.Name)
	print(table.concat(playerlogger, " "))
end
local playerPicker1 = math.random(1,#game.Players:GetPlayers())
local playerPicker2 = math.random(1,#game.Players:GetPlayers())

print(playerlogger[playerPicker1])
print(playerlogger[playerPicker2])
if playerPicker1 ~= playerPicker2 then
	print("player 1 is not player 2")
else repeat local playerPickerNewSelection = math.random(1,#game.Players:GetPlayers()) until playerPickerNewSelection ~= playerPicker2
	print(playerlogger[playerPickerNewSelection])
	print("player 1 is not player 2(after new selection")
	end

Basically, it will get 2 random players from the playerlogger table (playerPicker1 and playerPicker2) and check if the two picked players are the same player. If it is, then it will keep making a new variable (playerPickerNewSelection) until this new variable isn't an already selected player. The problem is that I can't seem to print(playerlogger[playerPickerNewSelection]), which I would need to print in order to see if the new selected player isn't the same one.

I would appreciate any help I receive, and thank you for reading this question.

1 comment

0

I'm looking into it give me a few mins

2 answers

✓ Accepted answer

I think this will work? Maybe, its completely untested so let me know.

wait(20)

local playerlogger = {}

for i, player in pairs(game.Players:GetPlayers()) do
	table.insert(playerlogger, player.Name)
	print(table.concat(playerlogger, " "))
end

local playerPicker1 = math.random(1,#game.Players:GetPlayers())
local playerPicker2 = math.random(1,#game.Players:GetPlayers())
print(playerlogger[playerPicker1])
print(playerlogger[playerPicker2])

while playerPicker1 == playerPicker2 do
	playerPicker2 = math.random(1,#game.Players:GetPlayers())
	print("player 1 is player 2, trying again")
	wait(.1)
end

print(playerlogger[playerPicker1])
print(playerlogger[playerPicker2])
print("player 1 is not player 2")

4 comments

0

I just realized we're not using the table properly... if this doesnt work tell me i wanna make some changes.

0

this seems to work. ill try this on my main script. thank you!

0

Not a problem, if it helped please accept the answer.

0

worked perfectly. tysm because I have been working on the main script for about 3 days.

The table seems to work fine, I don't fully know whats the issue, but I fixed it. ~~~~~~~~~~~~~~~~~ wait(20)

local playerlogger = {}

for i, player in pairs(game.Players:GetPlayers()) do table.insert(playerlogger, player.Name) print(table.concat(playerlogger, " ")) end local playerPicker1 = math.random(1,#game.Players:GetPlayers()) local playerPicker2 = math.random(1,#game.Players:GetPlayers()) local playerPickerNewSelection = math.random(1,#game.Players:GetPlayers()) print(playerlogger[playerPicker1]) print(playerlogger[playerPicker2]) if playerPicker1 ~= playerPicker2 then print("player 1 is not player 2") else repeat local playerPickerNewSelection = math.random(1,#game.Players:GetPlayers()) until playerPickerNewSelection ~= playerPicker2 print(playerlogger[playerPickerNewSelection]) print("player 1 is not player 2(after new selection") end

I tested it and it worked like I think it should but if not let me know.

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