Forcefields keep being created when I only wanted one

Hi I have recently been trying to code a shield system for a game I’m working on it was suppose to work as a forcefield being created when you hold down Q by equipping a tool. When the tool is equipped by holding down Q it triggers a forcefield to be created and when its unequipped the forcefield is destroyed but for some reason it creates 20+ forcefields and wont destroy any of them. I’ve tried putting in different if then statements as a precaution to stop it. But nothing works I think it has something to do with the RunService.Heartbeat system but it could be something else.
Here’s my 2 local scripts in the tool

forcefield creation:

local shield = script.Parent

shield.Equipped:Connect(function()
	print("works")
	local player = shield.Parent
	local force = Instance.new('ForceField')
	force.Parent = player
end)



shield.Unequipped:Connect(function()
	print("stopped")
	local player = shield.Parent.Parent
	local character = player.CharacterAdded:Wait()
	local force = character:GetChildren("ForceField")
	force:Destroy()
end)

equip tool script (which might be the problem)

local player = game.Players.LocalPlayer
local character = player.Character

local shield = script.Parent

if not character or not character.Parent then
	character = player.CharacterAdded:Wait()
end

local humanoid = character:WaitForChild("Humanoid")

-- Vars: Input
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local desiredKey = Enum.KeyCode.Q

local function keyIsDown() -- Returns if the key is down or not
	return UserInputService:IsKeyDown(desiredKey)
end

-- Vars: Tool
local tool = player.Backpack:WaitForChild("Shield") 
local handle = tool.Handle

local isHeld = false;

-- Functions
local function equipTool()
		if UserInputService:IsKeyDown(desiredKey) then
				if isHeld == false then
			humanoid:EquipTool(tool)
			local player = shield.Parent
			local force = Instance.new('ForceField')
			force.Parent = player
				else if isHeld == true then 
				return
					end
		end
	else 
		humanoid:UnequipTools(tool)
		local player = shield.Parent.Parent
		local character = player.CharacterAdded:Wait()
		local force = character:GetChildren("ForceField")
		force:Destroy()
			end
		end



if not	character:FindFirstChildWhichIsA('ForceField') then
	if not UserInputService:IsKeyDown(desiredKey) then
		RunService.Heartbeat:Connect(equipTool)
	end
end
2 Likes

Try this

Your ‘debounce’ (isHeld) isn’t set back to true after it’s been run.

1 Like

actually that semi solved the problem but when I stop holding down the q key the forcefield isn’t destroyed

1 Like

I just realized this script was disabled because I had put the code for creating forcefields in the equip tool script

Also how would I destroy the forcefield because what I have written doesn’t destroy it when the tool is unequipped

1 Like

Hmm, how about this: (my scripting knowledge is pretty basic, but here goes)

1 Like

It’s not working think I’m gonna mark your original post as the solution because that was my original problem and make another post on the dev forum

Your script is probably stuck waiting for the CharacterAdded:Wait(), unless the player respawns it will not progress past this point

shield.Unequipped:Connect(function()
	print("stopped")
	local player = shield.Parent.Parent

    -- we check if the character exists *or* we wait for it
	local character = player.Character or player.CharacterAdded:Wait()

    -- GetChildren was the wrong function, that gets ALL children as an array.
    -- use FindFirstChild to get a child by name, I use OfClass for this case.
	local force = character:FindFirstChildOfClass("ForceField")

    -- can return nil so we must check the forcefield exists before deleting it
    if force then
	    force:Destroy()
    end
end)
1 Like

Thank you so much both you and @Scottifly this has basically been solved but there’s still a bug with the debounce where I cant reequip the tool after using it but still thanks you guys are the greatest

1 Like

actually I fixed the problem with the debounce thing