local bad_guys = {"Someone whos bad", "a very bad person", "Bad guy"}
game.Players.PlayerAdded:Connect(function(plr)
for _,bad_dude in ipairs(bad_guys) do
if plr.Name ~= tostring(bad_dude) then
print('he is not bad!!!!!!!!')
end
end
end)
So, I made this code for fun. But here’s a thing.
‘he is not bad!!!’ will print 3 times because theres 3 usernames in the table.
I Know i can do if plr.Name ~= "Bad guy or "Some1 whos bad"
But that will be long and boring.
Is there a way to make the print() function run once for i,v pairs()?
local bad_guys = {"Someone whos bad", "a very bad person", "Bad guy"}
game.Players.PlayerAdded:Connect(function(plr)
for _,bad_dude in ipairs(bad_guys) do
if plr.Name ~= tostring(bad_dude) then
print('he is not bad!!!!!!!!')
return
end
end
end)
Oh I also want to make it print all the strings in the table by doing this
local bad_guys = {"Someone whos bad", "a very bad person", "Bad guy"}
game.Players.PlayerAdded:Connect(function(plr)
for _,bad_dude in ipairs(bad_guys) do
print(bad_dude)
if plr.Name ~= tostring(bad_dude) then
print('he is not bad!!!!!!!!')
return
end
end
end)
but it only prints 1 of them, and I dont want that to happen
game.Players.PlayerAdded:Connect(function(plr)
for _,bad_dude in ipairs(bad_guys) do
print(bad_dude)
if plr.Name ~= tostring(bad_dude) then
print('he is not bad!!!!!!!!')
return
end
end
for _,bad_dude in ipairs(bad_guys) do
if plr.Name ~= tostring(bad_dude) then
print('he is not bad!!!!!!!!')
return
end
end
end)
This code is quite inefficient. You only need one for loop and you do not need to iterate through the entire array. Your code also doesn’t work as intended, as it will not run the second snippet of code if they are not a bad guy.
Just make use of table.find and an else statement.
Players.PlayerAdded:Connect(function(Player)
if table.find(bad_guys, Player.Name) then
print("bad")
else
print("not bad")
end
end)