I have a script inside a part intended to remove an item from a player on a certain team, and give them a force field. However, if the player is holding the gun then the item cannot be removed from their inventory. 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)
The same tool can not be equipped and in the Players inventory at the same time. When a player equips a tool it is moved into their Character.
We can create a function to empty out both.
function removeTool(name,place) for _,v in pairs(place:GetChildren()) do if v:IsA("Tool") or v:IsA("HopperBin") and v.Name == name then v:Destroy() end end end local debounce = true script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") and debounce then debounce = false local plr = game.Players:GetPlayerFromCharacter(hit.Parent) f = Instance.new("Forcefield",hit.Parent) removeTool("Gun",plr.Backpack) removeTool("Gun",hit.Parent) end debounce = true end)