Touch event firing many times(SOLVED)

bumpaaaaaaaaaaaaaaaaaaaaaaaaaaaa

2 Likes

What about adding a debounce on the server-sided code?

1 Like

Actually what about moving the character away from the ball after they collide? It could be that the timer is too short and that the dead character is still touching the ball after the debounce ends

1 Like

what… read the code
30000000000000000000000000000000

1 Like

What if you tried disconnecting the event?

local connection
local db = false
game.ReplicatedStorage.Ball.OnClientEvent:Connect(function(speed)
	local char = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
	local ball = game.Workspace.Ball
	ball.Color = Color3.new(1, 0, 0)
	connection = ball.Touched:Connect(function(hit)
		if hit.Parent.Name == char.Name then
			if db == false then
				db = true
				if char:FindFirstChild("Tool") and char:FindFirstChild("Tool").DB.Value == true then
					char.Humanoid:LoadAnimation(script.Slash):Play()
					game.ReplicatedStorage.Effects:FireServer()
					game.ReplicatedStorage.Ball:FireServer(false)
					print(db)
				else
					game.ReplicatedStorage.Ball:FireServer(true)
				end
                if connection then
                   connection:Disconnect()
                   connection = nil
                end
			end
		end
	end)
	repeat 
		task.wait()
		game.Workspace.Ball.BodyVelocity.Velocity = ((char.HumanoidRootPart.Position-game.Workspace.Ball.Position)).Unit*speed
	until db == true
	connection:Disconnect()
	print("RAN")
	db = false
	ball.Color = Color3.new(99, 95, 98)
end)

Yes. Please read the code before replying

local connection
local db = false

game.ReplicatedStorage.Ball.OnClientEvent:Connect(function(speed)
    local char = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
    local ball = game.Workspace.Ball
    ball.Color = Color3.new(1, 0, 0)

    connection = nil 

    connection = ball.Touched:Connect(function(hit)
        if hit.Parent.Name == char.Name then
            if db == false then
                db = true
                if char:FindFirstChild("Tool") and char:FindFirstChild("Tool").DB.Value == true then
                    char.Humanoid:LoadAnimation(script.Slash):Play()
                    game.ReplicatedStorage.Effects:FireServer()
                    game.ReplicatedStorage.Ball:FireServer(false)
                    print(db)
                else
                    game.ReplicatedStorage.Ball:FireServer(true)
                end

                -- Disconnect the Touched event once it has been triggered
                if connection then
                    connection:Disconnect()
                    connection = nil
                end

                print("RAN")
                db = false
                ball.Color = Color3.new(99, 95, 98)
            end
        end
    end)

    repeat
        task.wait()
        game.Workspace.Ball.BodyVelocity.Velocity = ((char.HumanoidRootPart.Position - game.Workspace.Ball.Position)).Unit * speed
    until db == true
end)

I recommend adding a print() after

game.ReplicatedStorage.Ball.OnClientEvent:Connect(function(speed)

After you test it, can you please send a screenshot of your output if it doesn’t work?

1 Like

image
After a couple hits
issue might be on server ngl

1 Like

You could try doing something where you use a variable to tell it to not run code
Example:

local disabled = false
ball.Touched:Connect(function(hit)
	if not disabled then
		--code
	end
end)
1 Like
local connection
local db = false

local function onClientEvent(speed)
    print("OnClientEvent triggered")

    local char = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
    local ball = game.Workspace.Ball
    ball.Color = Color3.new(1, 0, 0)

    if connection then
        print("Already connected")
        return
    end

    connection = ball.Touched:Connect(function(hit)
        if hit.Parent.Name == char.Name then
            if db == false then
                db = true
                if char:FindFirstChild("Tool") and char:FindFirstChild("Tool").DB.Value == true then
                    char.Humanoid:LoadAnimation(script.Slash):Play()
                    game.ReplicatedStorage.Effects:FireServer()
                    game.ReplicatedStorage.Ball:FireServer(false)
                    print(db)
                else
                    game.ReplicatedStorage.Ball:FireServer(true)
                end

                -- Disconnect the Touched event once it has been triggered
                if connection then
                    connection:Disconnect()
                    connection = nil
                end

                print("RAN")
                db = false
                ball.Color = Color3.new(99, 95, 98)
            end
        end
    end)

    repeat
        task.wait()
        game.Workspace.Ball.BodyVelocity.Velocity = ((char.HumanoidRootPart.Position - game.Workspace.Ball.Position)).Unit * speed
    until db == true
end

game.ReplicatedStorage.Ball.OnClientEvent:Connect(onClientEvent)
1 Like

Your problem is the touched event fires every time the player or a part of the player touches the ball.
Your basic debounce is supposed to work like this:

Touched event fires.
Check if db == false to see if the debounce is active.
Make db true if it isn’t.
Perform your code.
Wait a short period of time (maybe a second or more) so the debounce stays true. This means the touched event can fire again, but it doesn’t run the code because of the if db == false check.
Make db false so the touched event can fire again.
End touched function.

Your code (see my **** comments in the code):

	connection = ball.Touched:Connect(function(hit)
		if hit.Parent.Name == char.Name then
			if db == false then
				db = true      --**** the touch fires, then db is made true
				if char:FindFirstChild("Tool") and char:FindFirstChild("Tool").DB.Value == true then
					char.Humanoid:LoadAnimation(script.Slash):Play()
					game.ReplicatedStorage.Effects:FireServer()
					game.ReplicatedStorage.Ball:FireServer(false)
					print(db)
				else
					game.ReplicatedStorage.Ball:FireServer(true)
				end
			end
		end
	end)
	repeat 
		task.wait()
		game.Workspace.Ball.BodyVelocity.Velocity = ((char.HumanoidRootPart.Position-game.Workspace.Ball.Position)).Unit*speed
	until db == true     -- ***** db is always true at this point, so it gets disconnected immediately
	connection:Disconnect()
	print("RAN")
	db = false
1 Like

dude please my code before saying something. That’s exactly what I did

	repeat 
		task.wait()
		game.Workspace.Ball.BodyVelocity.Velocity = ((char.HumanoidRootPart.Position-game.Workspace.Ball.Position)).Unit*speed
	until db == true     --db is ALWAYS true at this point, meaning you have only .03 seconds (1 frame) debounce

Dude, I did read your code.
You have no debounce wait with the way you have it set up. db is already true in your example, so the touched event code can run again immediately.

dude you set debounce to false just when it triggers


local connection
local db = false
game.ReplicatedStorage.Ball.OnClientEvent:Connect(function(speed)
	local char = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
	local ball = game.Workspace.Ball
	ball.Color = Color3.new(1, 0, 0)
	connection = ball.Touched:Connect(function(hit)
		if hit.Parent.Name == char.Name then
			if db == false then
				db = true
				if char:FindFirstChild("Tool") and char:FindFirstChild("Tool").DB.Value == true then
					char.Humanoid:LoadAnimation(script.Slash):Play()
					game.ReplicatedStorage.Effects:FireServer()
					game.ReplicatedStorage.Ball:FireServer(false)
					print(db)
				else
					game.ReplicatedStorage.Ball:FireServer(true)
				end
				elseif db == true then
				task.wait(0.5) -- THIS IS JUST FOR TEST YOU CAN CHANGE WAIT
				db = false
			end
		end
	end)
	repeat 
		task.wait()
		game.Workspace.Ball.BodyVelocity.Velocity = ((char.HumanoidRootPart.Position-game.Workspace.Ball.Position)).Unit*speed
	until db == true
	connection:Disconnect()
	print("RAN")
	ball.Color = Color3.new(99, 95, 98)
end)


this code should work

mark as solved if it works please

2 Likes

THANK YOU I THOUGHT DISCONNECTING THE LOOP WOULD STOP IT!!
THANK YOU SO MUCH
SORRY. I had to change some things cause it wasn’t working. Corrected code:

local connection
local db = false
game.ReplicatedStorage.Ball.OnClientEvent:Connect(function(speed)
	local char = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
	local ball = game.Workspace.Ball
	ball.Color = Color3.new(1, 0, 0)
	connection = ball.Touched:Connect(function(hit)
		if hit.Parent.Name == char.Name then
			if db == false then
				db = true
				if char:FindFirstChild("Tool") and char:FindFirstChild("Tool").DB.Value == true then
					char.Humanoid:LoadAnimation(script.Slash):Play()
					game.ReplicatedStorage.Effects:FireServer()
					print("FIRED")
					game.ReplicatedStorage.Ball:FireServer(false)
				else
					print("Fired")
					game.ReplicatedStorage.Ball:FireServer(true)
				end
				task.delay(0.5,function()
					db = false
				end)
			end
		end
	end)
	repeat 
		task.wait()
		game.Workspace.Ball.BodyVelocity.Velocity = ((char.HumanoidRootPart.Position-game.Workspace.Ball.Position)).Unit*speed
	until db == true
	connection:Disconnect()
	ball.Color = Color3.new(99, 95, 98)
end)

good luck :slight_smile: thank you for solved mark by the way

It caused another issue. When the remote event gets fired again it skips the loop cus it takes 0.5 seconds for db to get set to false

So basically what I’ve been saying the whole time, your debounce was always read as true, and @booga just wrote the line of code that fixed it…

The issue you’re having is you want the event to fire each time the ball is touched. You have to tune your debounce to work for you.
A player has many Parts so it touches lots of times even if it only looks like it is touching once. You can print after each Touched event so you can see what I mean.

	connection = ball.Touched:Connect(function(hit)
        print(hit.Name)   --shows the name of the Part that touched the ball.
        -- your code

If .5 seconds is too long change it to .4, or .3, or whatever works for you.

1 Like

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