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

Why is this inserted-object filtering script not working correctly?

Asked by 7 years ago
Edited 7 years ago

This script is supposed to check Workspace for newly inserted objects, see if those objects are within the parameters of an array, and to check if that newly created object is a player's character, if not either of those things, to remove it.

However, regardless of whether or not that object is within the parameters of the array, the script still deletes it.

Can someone please help?

Here's the script:

local allowed = {"Handle", "Bullet", "Rocket", "Camera", "Bomb", "Explosion", "Humanoid", "Part", "Missile", "Grenade", "Flashbang"}

workspace.ChildAdded:connect(function(item)
    Check(item)
end)

function Check(item)
    for i,v in pairs(allowed) do
        if v[i] == item.Name or item:FindFirstChild("Humanoid") then
                print('good')
        else    
            item:Destroy()
        end
    end
end

1 answer

Log in to vote
1
Answered by
Vulkarin 581 Moderation Voter
7 years ago

The reason it wasn't working is because you are checking the entire table and deleting it if it returns false (I insert a humanoid, everything in the table except for humanoid will delete it)

The solution would be to add a boolean to it

local allowed = {"Handle", "Bullet", "Rocket", "Camera", "Bomb", "Explosion", "Humanoid", "Part", "Missile", "Grenade", "Flashbang"}
local checked = false

workspace.ChildAdded:connect(function(item)
    Check(item)
end)

function Check(item)
    for _,v in pairs(allowed) do
        if v == item.Name or item:FindFirstChild("Humanoid") then
            checked = true
        end
    end
    if not checked then wait() item:Destroy() end
    checked = false
end
1
This works, thank you so much! CoolJohnnyboy 121 — 7y
0
No problem Vulkarin 581 — 7y
Ad

Answer this question