Help Needed with Touched Event

Anyone Know why this touched event is triggering more then once for each coin it adds more then one value. I made sure to delete the part so that the touched event doesn’t trigger more then once.

local TrapHandler = {}
local replicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")


-- Values
local values = replicatedStorage:WaitForChild("Values")
local coinValue = values:WaitForChild("Coins")

local coinObjectFolder = game.Workspace:WaitForChild("Coins")


local soundsFolder = replicatedStorage:WaitForChild("Sounds")
local coinSound = soundsFolder:WaitForChild("Coingrab")


TrapHandler.Coins =  coinValue

function TrapHandler.coinTouched()
	for i,p in ipairs(coinObjectFolder:GetChildren())do 
		p.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				p:Destroy()
				print("Part Destoyed")
				TrapHandler.Coins.Value = TrapHandler.Coins.Value + 1 
				print(p:IsA("BasePart"))
				coinSound:Play()
			end
		end)
	end
end

return TrapHandler
1 Like

Try this.

local TrapHandler = {}
local replicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")


-- Values
local values = replicatedStorage:WaitForChild("Values")
local coinValue = values:WaitForChild("Coins")

local coinObjectFolder = game.Workspace:WaitForChild("Coins")


local soundsFolder = replicatedStorage:WaitForChild("Sounds")
local coinSound = soundsFolder:WaitForChild("Coingrab")


TrapHandler.Coins =  coinValue

function TrapHandler.coinTouched()
	for i,p in ipairs(coinObjectFolder:GetChildren())do 
		p.Touched:Connect(function(hit)
        p.CanTouch = false
			if hit.Parent:FindFirstChild("Humanoid") then
				p:Destroy()
				print("Part Destoyed")
				TrapHandler.Coins.Value = TrapHandler.Coins.Value + 1 
				print(p:IsA("BasePart"))
				coinSound:Play()
			end
		end)
	end
end

return TrapHandler
1 Like