Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago
Edited 5 years ago

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)
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. ScriptGuider 5640 — 5y
0
Instead, it should be `table.remove(touched, i)` ScriptGuider 5640 — 5y
0
Ok, I will try it namespace25 594 — 5y
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. namespace25 594 — 5y
View all comments (11 more)
0
Its doing it once per item, as it should. DinozCreates 1070 — 5y
0
.----------. oof But it gives about 15 messages per item, whenever it should give 1. namespace25 594 — 5y
0
It shouldnt be? If the script is working as it should be just remove the print, its just for troubleshooting anyway. DinozCreates 1070 — 5y
0
Do you know where I could add a debounce in this? namespace25 594 — 5y
0
Do you want this to completely stop once it finds something? DinozCreates 1070 — 5y
0
Alright, I'm gonna add a break namespace25 594 — 5y
0
Yeah, whenever it finds something, stops. namespace25 594 — 5y
0
Thats what i was gonna recommend, a break on line... 42? That sounds good. DinozCreates 1070 — 5y
0
It still displays to many messages. namespace25 594 — 5y
0
Alright, I fixed it. Thank you. namespace25 594 — 5y
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 Gey4Jesus69 2705 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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)
Ad

Answer this question