Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to remove a forcefield when equipping a tool?

How do I remove the forcefield whenever a player equips a tool? This is in a server script.

function onEquipped(e)
if e.Parent ~= nil then 
if e.Parent:findFirstChild("ForceField") ~= nil 
		then e.Parent.ForceField:remove() 
		end
	end
end

script.Parent.Equipped:connect(onEquipped)

1 answer

✓ Accepted answer

First off, let's fix the indentation and replace findFirstChild with FindFirstChild, and connect with Connect, because those two functions are deprecated(or at least I think they are):

function onEquipped(e)
	if e.Parent ~= nil then 
		if e.Parent:FindFirstChild("ForceField") ~= nil then 
			e.Parent.ForceField:remove() 
		end
	end
end

script.Parent.Equipped:Connect(onEquipped)

Secondly, the Equipped event of the tool instance doesn't return the character of the player that has the tool. As a matter of fact, the tool is parented to the character of the player that equipped it, when it is equipped.

So, we know that when the player equips the tool, we can access their character by doing this:

local tool = script.Parent

function onEquipped()
	local forceField = tool.Parent:FindFirstChild("ForceField")
	if (forceField) then
		forceField:Destroy()
	end
end

tool.Equipped:Connect(onEquipped)

This'll destroy a ForceField, if it is found in the character.

2 comments

0

Shouldn't it be tool.Parent:FindFirstChild("ForceField")? There isnt any ForceField inside the tool but its in the character

0

Right, I have no clue why I put that.

This discussion is preserved as it was. New answers, comments, and votes are closed.