Touch Stop Not Working

my tool flings the player when the player clicks on the tool. when I am first Activating the tool it is flinging the player and after I come to fling the player again it is automatically flings him without I Activating the tool again.

how can i do that every time player Activates the tool it will fling the player? but only when player activates the tool. thanks for help i appreciate it a lot.

server script:

local Players = game:GetService("Players")
local Debounce = false

script.Parent.Push.OnServerEvent:Connect(function(WaIt)

		script.Parent.Handle.Touched:Connect(function(touch)

			print("touched")
			if touch.Parent:FindFirstChild("HumanoidRootPart") == nil then return end 

			local HRP = touch.Parent:FindFirstChild("HumanoidRootPart") 

			local Humanoid = touch.Parent:FindFirstChild("Humanoid")


			local BodyVelocity = Instance.new("BodyVelocity", HRP) 

			BodyVelocity.Velocity = Vector3.new(50,50,50) 

			wait(1)

			BodyVelocity:Destroy()

		end)
end)

local script:

local Debounce = false

script.Parent.Activated:Connect(function()
	local humanoid = game.Players.LocalPlayer.Character.Humanoid
	local Anim = script.Parent:FindFirstChild("Animation")

	local ThrowChairAnim = humanoid:LoadAnimation(Anim)
	
	if Debounce == false then
		
		Debounce = true
		ThrowChairAnim:Play()
		wait(0.5)
		script.Parent.Push:FireServer(1)
		print("fired")
		wait(1)

		Debounce = false
	end
end)

Just turn on CanTouch every time you fire the event
(Basically set CanTouch to true every time event fires. Let’s say:

remoteWhenAnimPlays:FireServer()
anim.Stopped:Wait()
remoteWhenAnimStops:FireServer()
remoteWhenAnimPlays.OnServerEvent:Connect(function()
hitbox.CanTouch = true
end)

remoteWhenAnimStops.OnServerEvent:Connect(function()
hitbox.CanTouch = false
end)
hitbox.Touched:Connect(function(touch)
hitbox.CanTouch = false
--- function
end)


Why not just use debounce?

local debounce = false

After the touched function:

debounce = true
--code, then set debounce to false again

Because it’s not accurate enough

Why is it not accurate enough?

1 Like

Another mistake you’re doing here is that you’re nesting the Touched event in the OnServerEvent event. This means that every time the remote event gets called, you will connect a duplicate Touched event, which will make it fire n+1, n being the number of times it has already been called.

Connecting an event returns a RBXScriptConnection object that with a :Disconnect() method that you can use to prevent this.

local Players = game:GetService("Players")
local Debounce = false
local connection = nil

script.Parent.Push.OnServerEvent:Connect(function(WaIt)

		connection = script.Parent.Handle.Touched:Connect(function(touch)
			if Debounce == true then return end
			Debounce = true
			print("touched")
			if touch.Parent:FindFirstChild("HumanoidRootPart") == nil then return end 

			local HRP = touch.Parent:FindFirstChild("HumanoidRootPart") 

			local Humanoid = touch.Parent:FindFirstChild("Humanoid")


			local BodyVelocity = Instance.new("BodyVelocity", HRP) 

			BodyVelocity.Velocity = Vector3.new(50,50,50) 

			wait(1)

			BodyVelocity:Destroy()
			Debounce = false
			connection:Disconnect()
		end)
end)
2 Likes

actually, i was sleepy back then, i didn’t realised what he said rly lolol. Apologies