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

For i, v loop doesn't stop where it should and just keeps on going even after an "else"?

Hello! I've got a bit of a problem with my ban command script, and I've narrowed it down to this function with the help of print debugging. My issue with this is that I'm trying to make it so that moderators within the game aren't able to ban eachother. However, this doesn't seem to work.

local function banByUserID(userid)
	local nameofUser = game.Players:GetNameFromUserIdAsync(userid)
	for i,v in pairs(moderators) do
		if v == nameofUser then
			print ("Nice try. /".. nameofUser.. "/ is a moderator and can't be banned!")
		else -- should stop at this once has detected a moderator
			if bansDataStore:GetAsync("banned_"..userid,1) then
				print ("User ".. nameofUser.. " has already been banned!")
			else
				if nameofUser then
					bansDataStore:SetAsync("banned_"..userid,1)
					if game.Players:FindFirstChild(nameofUser) then
						game.Players:FindFirstChild(nameofUser):Kick(reason)
					end
					print ("User ".. nameofUser.. " has been banned!")
				end
			end
		end
	end
end

It should stop at the "else" once it has detected a moderator, but it keeps going on, resulting in this and banning the moderator anyway. image showing output

The "moderator" variable is just a regular table with strings of people's usernames.

1 answer

✓ Accepted answer

You can break a loop with the built in function 'break'

for i, v in pairs() do
print('_')
break
end

2 comments

0

Can't believe I missed such an easy solution, thank you!

0

You're welcome.

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