The check is triggered several times

local player = game:GetService("Players").LocalPlayer
local tween = game:GetService("TweenService")
local debounce = true

local function dynamiteTouched()
	if script.Parent:WaitForChild("DynamiteValue").Value ~= nil then
		script.Parent:WaitForChild("DynamiteValue").Value.Touched:Connect(function(hit)
			if hit.Name == "HitBox1Train" then
				wait(0.3)
				
				local cloneText = player.PlayerGui.PowerUp.plusPower:Clone()
				cloneText.Parent = player.PlayerGui.PowerUp
				cloneText.Visible = true
				cloneText.Name = "plusPowerClone"
				cloneText.countText.Text = player.Data.PlusDamage.Value
				cloneText.Position = UDim2.new(math.random(100, 800)/1000, 0, math.random(200, 800)/1000, 0)				
				local targetSize = UDim2.new(0.093, 0, 0.181, 0)
				local tweenInfo = TweenInfo.new(0.3)
				
				local damageCount = player.PlayerGui.ShowStatsUnder.Damage
				local targetPosition1 = UDim2.new(0.4, 0, -0.02, 0)
				local targetPosition2 = UDim2.new(0.4, 0, 0, 0)
				local tweenInfo1 = TweenInfo.new(0.9)
				
				local tweenPosition1 = tween:Create(damageCount, tweenInfo1, {Position = targetPosition1})
				
				tweenPosition1:Play()
				
				
				local tweenSize = tween:Create(cloneText, tweenInfo, {Size = targetSize})
				tweenSize:Play()
				
				player.Data.DamageCount.Value = player.Data.DamageCount.Value + player.Data.PlusDamage.Value
				
				wait(0.1)
				
				local tweenPosition2 = tween:Create(damageCount, tweenInfo1, {Position = targetPosition2})
				tweenPosition2:Play()
				
				wait(0.8)
				
				cloneText:Destroy()
			end
			
			if hit.Name == "Sand_1" and debounce == true then
				debounce = false
				print("Hit")
				print(debounce)
				debounce = true
			end
		end)
	end
end
script.Parent:WaitForChild("DynamiteValue"):GetPropertyChangedSignal("Value"):Connect(dynamiteTouched)

So I have a script that checks which desk I touched when I throw dynamite. The problem is this. When it touches a part, it prints several times. But I need to print only once. What kind of check should I put on it?

1 Like

Try this

if script.Parent:WaitForChild("DynamiteValue").Value ~= nil and script.Parent:WaitForChild("DynamiteValue").Value ~= false then

Because it only checks if it get changed, it doesn’t care if it on true or false

local player = game:GetService("Players").LocalPlayer
local tween = game:GetService("TweenService")
local debounce = true

local function dynamiteTouched()
	if script.Parent:WaitForChild("DynamiteValue").Value ~= nil and script.Parent:WaitForChild("DynamiteValue").Value ~= false then
		script.Parent:WaitForChild("DynamiteValue").Value.Touched:Connect(function(hit)
			if hit.Name == "HitBox1Train" then
				wait(0.3)
				
				local cloneText = player.PlayerGui.PowerUp.plusPower:Clone()
				cloneText.Parent = player.PlayerGui.PowerUp
				cloneText.Visible = true
				cloneText.Name = "plusPowerClone"
				cloneText.countText.Text = player.Data.PlusDamage.Value
				cloneText.Position = UDim2.new(math.random(100, 800)/1000, 0, math.random(200, 800)/1000, 0)				
				local targetSize = UDim2.new(0.093, 0, 0.181, 0)
				local tweenInfo = TweenInfo.new(0.3)
				
				local damageCount = player.PlayerGui.ShowStatsUnder.Damage
				local targetPosition1 = UDim2.new(0.4, 0, -0.02, 0)
				local targetPosition2 = UDim2.new(0.4, 0, 0, 0)
				local tweenInfo1 = TweenInfo.new(0.9)
				
				local tweenPosition1 = tween:Create(damageCount, tweenInfo1, {Position = targetPosition1})
				
				tweenPosition1:Play()
				
				
				local tweenSize = tween:Create(cloneText, tweenInfo, {Size = targetSize})
				tweenSize:Play()
				
				player.Data.DamageCount.Value = player.Data.DamageCount.Value + player.Data.PlusDamage.Value
				
				wait(0.1)
				
				local tweenPosition2 = tween:Create(damageCount, tweenInfo1, {Position = targetPosition2})
				tweenPosition2:Play()
				
				wait(0.8)
				
				cloneText:Destroy()
			end
			
			if hit.Name == "Sand_1" and debounce == true then
				debounce = false
				
				print("Hit")
				print(debounce)
				hit:FindFirstChild("Current").Value -= player.Data.DamageCount.Value
				debounce = true
			end
		end)
	end
end
script.Parent:WaitForChild("DynamiteValue"):GetPropertyChangedSignal("Value"):Connect(dynamiteTouched)

You mean like this? If so, nothing has changed.

Could you show me the Objects on the Explorer, so I can understand this more?

If you mean the dynamite I’m throwing, here he is
Снимок экрана 2023-06-29 в 20.25.42

I don’t understand how a Value can be touched?

script.Parent:WaitForChild("DynamiteValue").Value.Touched:Connect(function(hit)

watch. This is the value, this is roughly the name of the subject. I’m checking to see if THIS item has touched the right thing. Like if dynamite was touched, then call the function, and if a hand was touched, then don’t call it.

Maybe do in the TouchedFunction so (if DynamiteValue is the Value)

script.Parent:WaitForChild("DynamiteValue").Value.Touched:Connect(function(hit)
	script.Parent:WaitForChild("DynamiteValue").Value = false -- ADDED TO SCRIPT
	if hit.Name == "HitBox1Train" then
		wait(0.3)

		local cloneText = player.PlayerGui.PowerUp.plusPower:Clone()
		cloneText.Parent = player.PlayerGui.PowerUp
		cloneText.Visible = true
		cloneText.Name = "plusPowerClone"
		cloneText.countText.Text = player.Data.PlusDamage.Value
		cloneText.Position = UDim2.new(math.random(100, 800)/1000, 0, math.random(200, 800)/1000, 0)				
		local targetSize = UDim2.new(0.093, 0, 0.181, 0)
		local tweenInfo = TweenInfo.new(0.3)

		local damageCount = player.PlayerGui.ShowStatsUnder.Damage
		local targetPosition1 = UDim2.new(0.4, 0, -0.02, 0)
		local targetPosition2 = UDim2.new(0.4, 0, 0, 0)
		local tweenInfo1 = TweenInfo.new(0.9)

		local tweenPosition1 = tween:Create(damageCount, tweenInfo1, {Position = targetPosition1})

		tweenPosition1:Play()


		local tweenSize = tween:Create(cloneText, tweenInfo, {Size = targetSize})
		tweenSize:Play()

		player.Data.DamageCount.Value = player.Data.DamageCount.Value + player.Data.PlusDamage.Value

		wait(0.1)

		local tweenPosition2 = tween:Create(damageCount, tweenInfo1, {Position = targetPosition2})
		tweenPosition2:Play()

		wait(0.8)

		cloneText:Destroy()
	end

	if hit.Name == "Sand_1" and debounce == true then
		debounce = false

		print("Hit")
		print(debounce)
		hit:FindFirstChild("Current").Value -= player.Data.DamageCount.Value
		debounce = true
	end
end)

No, it doesn’t work. I think it’s better to add some wait(), so it doesn’t read twice (I mean add it to the part with hit.Name)

I don’t know how you made the system, but where is the script that does the DynamiteTouched Value to the opposite value so basically true?

Yes, like I thought I would, I just put wait(1) and it all worked out.

if hit.Name == "Sand_1" and debounce == true then
				debounce = false
				print("Hit")
				print(debounce)
				debounce = true
			end

You check if hitname is sand_1, and if debounce. Then you set debounce to false, but you immediately set it back to true. The goal of debounce is to add some time in between, so add a wait in between print(“Hit”) and print(debounce). That’ll ensure it doesn’t trigger multiple times.