Clicking a ClickDetector activates forever

So I have a grave Part that turns transparent when I click the ClickDetector attached to it, but only If I have a shovel tool in my Backpack. The problem is that if I click the Part and not have the shovel in my Backpack, I can pick up the shovel after clicking the Part and the Part still turns transparent even tough I clicked it before picking up the shovel. I have attached a video so its easier to understand:

I’m trying to make it so that if you pick up the shovel after clicking the grave Part, the Part doesn’t turn transparent and you have to click the grave Part again to turn it transparent

robloxapp-20230624-1323167.wmv (3.8 MB)

Here is my script:

game.Workspace.graveyard.gravevdig.ClickDetector.MouseClick:Connect(function(plr)

local shovel = plr:FindFirstChild("Backpack"):WaitForChild("sho")

if shovel then
	
	game.SoundService.creepysound:Play()
	game.Workspace.graveyard.gravevdig.Transparency = 1
	game.Workspace.graveyard.gravevdig.CanCollide = false
	
	game.ReplicatedStorage.GraveText:FireAllClients(20)
	
	wait(6)
	
	game.SoundService["fast wind woosh sound effect"]:Play()	
	
if not shovel then
	
	print("no shovel")
		
	end
end

end)

replace the line

local shovel = plr:FindFirstChild("Backpack"):WaitForChild("sho")

with

local shovel = plr:FindFirstChild("Backpack"):FindFirstChild("sho")

WaitForChild will yield until it finds a child with the name “sho” in the inventory which is why the part will turn transparent after picking up the shovel. FindFirstChild will return nil if it cant find a child of name “sho” in the inventory and the rest of your script is made to account for that. (nil is falsy, so anything inside the “if shovel then” block wont run if there is no shovel)

1 Like

It appears this part if not shovel then could be replaced as elseif not shovel then; better yet with just an else.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.