Tool Won't Function When Equipped

Hello! I’m Clara, and I’ve been trying to make it to where whenever you touch a part (when you have a tool either in your backpack or equipped), the touched part’s transparency goes to 0 and the CanCollide is set to false, then the tool gets destroyed.

The problem I’ve been experiencing is that when the tool is in the backpack it works, but when it’s not in the backpack it doesn’t work.

I’ve already tried to look for solutions on the forum and I haven’t found any or I just didn’t search hard enough.

Anyways, this is my code (a server script that’s in the part):

local debounce = false
local part = script.Parent
local Players = game:GetService("Players")

part.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		if hit.Parent:FindFirstChild("Humanoid") then
			local c = hit.Parent
			local player = Players:GetPlayerFromCharacter(c)
			if player.Backpack.Key1 then
				print("at if")
				part.CanCollide = false
				part.Transparency = 0
				player.Backpack.Key1:Destroy()
			elseif c.Key1 then
				print("at elseif")
				part.CanCollide = false
				part.Transparency = 0
				c.Key1:Destroy()
			else
				print("at else")
			end
			wait(5)
			debounce = false
		end
	end
end)
3 Likes

Try using :FindFirstChild()

elseif c:FindFirstChild("Key1") then

And make sure to check the output to know exactly where your code stops.

2 Likes

That didn’t work. And I’m not sure where the code stopped.

2 Likes

Did “at elseif” get printed in the output? If not, that means the issue is in the condition.

1 Like

Nope, it did not print anything. So it is the condition then.

1 Like

When you equip your tool, it appears in your character model, so make checking in Character’s model too

And yes, use Instance:FindFirstChild(Name) , because if tool is not in the inventory, it will give you an error

Yep, it’s in the character model when I equip the tool.

local debounce = false
local part = script.Parent
local Players = game:GetService("Players")

part.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		if hit.Parent:FindFirstChild("Humanoid") then
			local c = hit.Parent
			local player = Players:GetPlayerFromCharacter(c)
			if player.Backpack:FindFirstChild("Key1") then
				print("at if")
				part.CanCollide = false
				part.Transparency = 0
				player.Backpack.Key1:Destroy()
			elseif c:FindFirstChild("Key1")  then
				print("at elseif")
				part.CanCollide = false
				part.Transparency = 0
				c.Key1:Destroy()
			else
				print("at else")
			end
			wait(5)
			debounce = false
		end
	end
end)

that must work I guess

Still nope. I copied and pasted.

I figured out the problem.
You change debounce to true even when object is not a human, but you change it back to false once actual human touches it.

did you mean doing this:

	if debounce == false then
		debounce = true
		if hit.Parent:FindFirstChild("Humanoid") then
			debounce = false
local part = script.Parent
local Players = game:GetService("Players")

part.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		print(hit,hit.Parent)
		if hit.Parent:FindFirstChild("Humanoid") then
			print('ok')
			local c = hit.Parent
			local player = Players:GetPlayerFromCharacter(c)
			if player.Backpack:FindFirstChild("Key1") then
				print("at if")
				part.CanCollide = false
				part.Transparency = 0
				player.Backpack.Key1:Destroy()
			elseif c:FindFirstChild("Key1")  then
				print("at elseif")
				part.CanCollide = false
				part.Transparency = 0
				c.Key1:Destroy()
			else
				print("at else")
			end
			wait(5)
		end
		debounce = false
	end
end)

Here is fixed version, look into that

1 Like