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

How can I remove an item a player is holding?

Asked by
jacobwow 140
9 years ago

Here I have a script that is to remove a gun from someones backpack OR to remove it from them if they are holding it, if they are on a certain team, it also gives them a force field. However, If the player has the gun equipped it will not remove the gun. How can I fix this?

local debounce = false

function onTouched(hit) 
    if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then 
        if game.Players:GetPlayerFromCharacter(hit.Parent).TeamColor == script.Parent.BrickColor then
            local Player = game.Players:GetPlayerFromCharacter(hit.Parent) 
            if debounce == false then
                debounce = true
            local c = Instance.new("ForceField")
            c.Parent = hit.Parent
            if Player.Backpack.Gun == Player.Backpack.Gun then
                Player.Backpack.Gun:Remove()
                print("Gun Removed From BackPack")
                    wait(.1)
            debounce = false
            else if Player.Character.Gun == Player.Character.Gun then
                Player.Character.Gun:Remove()
                print("Gun Unequiped")
            wait(.1)
            debounce = false
                    end
                end
            end
        end
    end 
end 

script.Parent.Touched:connect(onTouched)
0
elseif shouldn't have a space. woodengop 1134 — 9y

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

Alright so first off, use variables. You typed that big GetPlayerFromCharacter line three times.

Keep your variable styles consistent -- capitalized, or not capitalized. You have Player, but then you also have hit and debounce.

Combine if statements with and.

Use the second argument of Instance.new to shorten your code.

Wait until everything is finished until enabling the script.

Don't put the space between the else and the if. What that's doing is basically just the same thing as what else does -- except with an if statement shoved inside. You can't have mutiple else's and you need an extra end. It's just a bad habit to get into.

So with these basic problems fixed (although it still won't work, because we haven't addressed the main issue yet), the script should look something like this:

local debounce = false

function onTouch(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

    if player and player.TeamColor == script.Parent.BrickColor and not debounce then
        debounce = true
        Instance.new("ForceField", player.Character)
        --Rest of code
        debounce = false
    end
end

Now for the main problem.

if Player.Backpack.Gun == Player.Backpack.Gun then

Alright, this just makes no sense. Of course the gun will equal the gun. This is one of the basic laws of logic. My cat isn't a parachute. My computer isn't a pizza. Something is what it is. A equals A.

But what if the gun is nil? Then you have a problem. You just indexed a nil value, and the code will error. You need to use FindFirstChild. Since it works like a function, instead of throwing an error if the child doesn't exist, it just returns nil, so it's useful in situations like these. Just keep in mind that it's only really useful in if statements.

local debounce = false

function onTouch(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

    if player and player.TeamColor == script.Parent.BrickColor and not debounce then
        debounce = true
        Instance.new("ForceField", player.Character)
        if player.Backpack:FindFirstChild("Gun") then
            --If it passed above, we know it exists.
            --Use Destroy, Remove is deprecated.
            player.Backpack.Gun:Destroy()
        elseif player.Character:FindFirstChild("Gun") then
            player.Character.Gun:Destroy()
        end
        wait(0.1)
        debounce = false
    end
end
Ad

Answer this question