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

For loop only checking first object in table?

Okay so I have a for loop checking if the player speaking is in the Admins array. It only check the players name against the first object in the array. what's going wrong? No errors are being outputted. Code:

local Admins = {"MrLonely1221","Player1"};

function isAdmin(plr)
	for i, v in pairs(Admins) do
			print(i,v);
			if plr == v then
				return true;
			else
				if GroupAdmin then
					if game.Players:FindFirstChild(plr):IsInGroup(GroupID) then
						if game.Players:FindFirstChild(plr):GetRankInGroup(GroupID) >= GroupRank then
							return true;
						end;
					end;
				end;
				print("No no no");
				return false;
			end;
		end;
end;
game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg)
		if isAdmin(player.Name) then
			print("hi");
		end;
	end;
end;

It prints "No no no" and then returns false. Any idea as to why it does this? Here is the output: 1 MrLonely1221 No no no

1 answer

✓ Accepted answer

The main problem is that when you return a function, the function and all loops inside it stop.

Simply move the return false to the bottom of the function, so that it can loop through all of the players first.


Optimization

Instead of calling the isAdmin() with player.Name, call it using player, then just use plr.Name inside the function. This will make your code more efficient, since the script will call FindFirstChild() every time it run. Better to just define plr as the actual player.

As well as that use the if statement inside the PlayerAdded event callback outside of Chatted, because that means that the script will not have to check groups every time someone types a message, and is more efficient in general.


Summing it up with code

local Admins = {"MrLonely1221","Player1"};

function isAdmin(plr)
	for i, v in pairs(Admins) do
		print(i,v);
		if plr.Name == v then
			return true;
		end;
	end;
	if GroupAdmin then
		if game.Players:FindFirstChild(plr):IsInGroup(GroupID) then
			if game.Players:FindFirstChild(plr):GetRankInGroup(GroupID) >= GroupRank then
					return true;
				end;
			end;
		end;
	end;
	print("Not an admin!")
	return false;
end;

game.Players.PlayerAdded:connect(function(player)
	if isAdmin(player) == true then
		print(player.Name.. " is an admin!")
		player.Chatted:connect(function(msg)
			print(player.Name.. " said: " ..msg)
		end;
	end;
end;

TIP:

Use TAB to format, not space. TAB is x1000000000000000 better, in terms of consistency, simplicity, and ease of use.


Hope I helped!

~TDP

2 comments

0

I do use tabs, not spaces. And I was tired when trying to make this. I figured out the problem without reading all the way through your solution. Thank your for the answer though, I moved my return false outside the for and then read through your answer and saw it. Thank you though. I'm going to select this as the answer, because it is, and so that others can see that this does help and has been f

0

No problem lol

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