Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Unable to print after a repeat loop?

Asked by 5 years ago

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.

0
I'm looking into it give me a few mins moo1210 587 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

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")
0
I just realized we're not using the table properly... if this doesnt work tell me i wanna make some changes. DinozCreates 1070 — 5y
0
this seems to work. ill try this on my main script. thank you! TheLionLiar 39 — 5y
0
Not a problem, if it helped please accept the answer. DinozCreates 1070 — 5y
0
worked perfectly. tysm because I have been working on the main script for about 3 days. TheLionLiar 39 — 5y
Ad
Log in to vote
0
Answered by
moo1210 587 Moderation Voter
5 years ago

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.

Answer this question