Brick isn't touched yet the event still runs!

Good Afternoon!

My friend and I are remastering an old game of mine and we came across a bug with touching a brick. What happens is that it plays the audio even though you haven’t touched the brick at all. Here’s the script:

local Cleetus = game.Workspace.Trigger
local Debounce = false

script.Parent.Touched:Connect(function(hit)

Debounce = true

wait(0.5)

game.Workspace.Sound:Play()

wait(3)

game.Workspace.Sound:Stop()

Debounce = false

end)

Please help! (Ps. there was no error to go off of.)

touched counts whenever the part is touched by any part. Which its probably activating that first time due to the part touching whatever ground its on. To avoid this add a check to see if its the player.

1 Like

What section should I change? And what should be replaced with what.

The problem is that you don’t have any checks. If you only want a player to be able to fire the touched event, you will need to do something like this

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Gets the player from its character

		if player then -- If a player hits it then continue the code

			game.Workspace.Sound:Play()
			wait(3)
			game.Workspace.Sound:Stop()

		end
		debounce = false
	end
end)

You don’t get an error because nothing is wrong with the code. You just need to add a check. If statements are what you’re looking for. I recommend reading up on them here.

If you have any questions feel free to ask me!

This Worked! Thank you so much!