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

Loops inside loops not functioning correctly?

I have no clue why this doesn't work for me, but what I have is a loop inside a loop. So, the first loop is to get the children of game.Players and the next loop is to get the children of the backpack of said children, but it doesn't seem to work.

I have placed prints throughout to see what is going wrong, including some pcalls aswell, but nothing has helped. Script Analysis has not worked either.

What gets printed

Loaded.
Loaded
Hasnt messed up yet
Still good!
Checking (x2)
1

The code

print'Loaded'

print'Hasnt messed up yet'

function checkTools()
	print'Checking'
	for i,v in pairs(game.Players:GetChildren()) do
		print'1'
		repeat wait() until v.Backpack		
		for u,x in pairs(v.Backpack:GetChildren()) do
			print'2'	
			if not x:FindFirstChild("Exempt") then
				print'3'
				v:Kick()
				warn"Exploit Detected or this system doesn't really work!"
			end
		end
	end
end

print'Still good!'

game.Players.PlayerAdded:connect(function(player)
	repeat wait() until player.Backpack
	local success,msg = pcall(checkTools)
	if success then
	else
		print(msg)
	end
end)

local success,msg = pcall(checkTools)
if success then
else
	print(msg)
end

for i,v in pairs(game.Players:GetPlayers()) do
	v.Chatted:connect(function(msg)
		if v:GetRankInGroup(3026610) >= 52 then
			if msg == "checkTools" then
				local success,msg = pcall(checkTools)
				if success then
				else
					print(msg)
				end
			end
		end
	end)
end

while wait(60) do
	local success,msg = pcall(checkTools)
	if success then
	else
		print(msg)
	end
end

Any help is appreciated, thank you.

1 answer

✓ Accepted answer

There is no need to do check this way as you can simply use ChildAdded

-- check for bad tools
local function chkTool(obj, plr)
	if obj:IsA('Tool') then
		if not obj:FindFirstChild('Exempt') then
			print('Bad tool', plr)
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)	
	plr.CharacterAdded:Connect(function(charModel) -- add events when the player spawns
		charModel.ChildAdded:Connect(function(ins) -- for the character
			chkTool(ins, plr)
		end)
		plr:WaitForChild('Backpack').ChildAdded:Connect(function(ins) -- for the backpack
			chkTool(ins, plr)
		end)
	end)
end)

I have not had any problems using your code so I do not know why your code does not seem to work for you.

I hope this helps.

Side notes:-

I would not rely on using a system like this as all I would need to do is add a child named 'Exempt', you could create your own tool giver script so the player cannot add tools themselves. This can be done by storing a list of what tools a player should have and comparing them with what they currently have.

There are a lot of other methods you could use for this kind of task.

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