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

Loop giving multiple messages when it should give 1 then remove?

I have a scanning system to check for illegal tools in a player's backpack and it works it just doesn't remove the players that have been displayed from the table. I need help to fix this:

It does remove but it gives a bunch of messages: [SUSPECTSHOT108]: ARMED when it should give one then remove the player's name from the table.

Server Script:

local touched = {}
local detectable = {'G18', 'G17', 'M-18', 'Minigun', 'Sword'}

local function SearchTable(tab, val)
	local bool = false
	for _,v in ipairs(tab) do
		if v == val then
			bool = true
		end
	end
	return bool
end

script.Parent.Touched:Connect(function(hit)
	local Player
	pcall(function()
		Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	end)
	
	if Player then
		if SearchTable(touched, Player.Name) == true then
			return
		else
			table.insert(touched, Player.Name)
		end
	end
	
	for i,v in pairs(touched) do
		local target = game.Players:FindFirstChild(v)
		print (target.Name..' is in the table!')
		for _,d in pairs(detectable) do
			print (d..' can be detected!')
			if target.Backpack:GetChildren() == nil then return end
			for _,t in pairs(target.Backpack:GetChildren()) do
				if t.Name == d then
					local Clone = script:WaitForChild("Example"):Clone()
					local Name = string.upper(target.Name)
					Clone.Text = "["..Name.."]: ARMED"
					Clone.TextColor3 = Color3.fromRGB(255, 0, 0)
					Clone.Parent = script.Parent.Parent.Status.Text.Frame
					print (target.Name..' is armed!')
					table.remove(touched, i) -- %Remove%
				else
					local Clone = script:WaitForChild("Example"):Clone()
					local Name = string.upper(target.Name)
					Clone.Text = "["..Name.."]: ARMED"
					Clone.Parent = script.Parent.Parent.Status.Text.Frame
					print (target.Name..' is unarmed!')
					table.remove(touched, i) -- %Remove%
				end
			end
		end
	end
end)

15 comments

0

Your uncertainty is correctly placed; you're not using table.remove properly. The second argument is always the position in the array that you want to remove; not the value in it's place.

0

Instead, it should be table.remove(touched, i)

0

Ok, I will try it

0

Ok, I edited it. It might be removing it from the table, but it gives a bunch of [SUSPECTSHOT108]: ARMED I need it to only do that once and then remove the name.

0

Its doing it once per item, as it should.

0

.----------. oof But it gives about 15 messages per item, whenever it should give 1.

0

It shouldnt be? If the script is working as it should be just remove the print, its just for troubleshooting anyway.

0

Do you know where I could add a debounce in this?

0

Do you want this to completely stop once it finds something?

0

Alright, I'm gonna add a break

0

Yeah, whenever it finds something, stops.

0

Thats what i was gonna recommend, a break on line... 42? That sounds good.

0

It still displays to many messages.

0

Alright, I fixed it. Thank you.

0

:GetChildren() wont be nil, it will just be an empty table btw. and u dont need to use pcall to find the player. just run an if statement to make sure it isnt nil

1 answer

Alright, I fixed it! If you are wondering how:

local touched = {}
local detectable = {'G18', 'G17', 'M-18', 'Minigun', 'Sword'}
local cooldown = false

local function SearchTable(tab, val)
	local bool = false
	for _,v in pairs(tab) do
		if v == val then
			bool = true
			break
		end
	end
	return bool
end

local function AddLabel(target, d, t)
	if t == 'armed' then
		local Clone = script:WaitForChild("Example"):Clone()
		local Name = string.upper(target.Name)
		Clone.Text = "["..Name.."]: ARMED | DETECTED: "..d
		Clone.TextColor3 = Color3.fromRGB(255, 0, 0)
		Clone.Parent = script.Parent.Parent.Status.Text.Frame
	else
		local Clone = script:WaitForChild("Example"):Clone()
		local Name = string.upper(target.Name)
		Clone.Text = "["..Name.."]: UNARMED | DETECTED: "..d
		Clone.Parent = script.Parent.Parent.Status.Text.Frame
	end
end

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		if not cooldown then
			cooldown = true
			
			local Player
			
			pcall(function()
				Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			end)
			
			if Player then
				if SearchTable(touched, Player.Name) == true then
					return
				else
					table.insert(touched, Player.Name)
				end
			end
			
			for i,v in pairs(touched) do
				local target = game.Players:FindFirstChild(v)
				table.remove(touched, i)
				for _,d in pairs(detectable) do
					if target.Backpack:GetChildren() == nil then return end
					for _,t in pairs(target.Backpack:GetChildren()) do
						if t.Name == d then
							AddLabel(target, d, 'error')
							break
						--[[elseif t.Name ~= d then
							AddLabel(target, d, 'unarmed')
							break]]
						end
					end
				end
			end
			wait(4)
			cooldown = false
		end
	end
end)

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