Touched Event not very reliable?

I’m making a soccer game and I have a goal and I want it to so when the ball is in the goal it counts as a goal, everything works but sometimes the touch event simply doesn’t work and the ball roams freely inside of the goal. Here’s my script.

local goalB = workspace.GoalB
local goalA = workspace.GoalA
local Team1Value = game.ReplicatedStorage.Team1Value
local Team2Value = game.ReplicatedStorage.Team2Value
local ball = workspace.SoccerBall
local originalPosition = workspace.BallSpawn
local debounce = false

goalA.Touched:Connect(function(hit)
if hit == ball then
if not debounce then
debounce = true
Team1Value.Value = Team1Value.Value + 1
ball.Hitbox.Particle.Score:Emit()
wait(0.5)
ball.CFrame = originalPosition.CFrame
ball.Anchored = true
end
wait(1)
ball.Anchored = false
debounce = false
end
end)

goalB.Touched:Connect(function(hit)
if hit == ball then
if not debounce then
debounce = true
Team2Value.Value = Team2Value.Value + 1
ball.Hitbox.Particle.Score:Emit()
wait(0.5)
ball.CFrame = originalPosition.CFrame
ball.Anchored = true
end
wait(1)
ball.Anchored = false
debounce = false
end
end)

Put your function within the debounce like this:

local goalB = workspace.GoalB
local goalA = workspace.GoalA
local Team1Value = game.ReplicatedStorage.Team1Value
local Team2Value = game.ReplicatedStorage.Team2Value
local ball = workspace.SoccerBall
local originalPosition = workspace.BallSpawn
local debounce = false

goalA.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		if hit == ball then
			Team1Value.Value = Team1Value.Value + 1
			ball.Hitbox.Particle.Score:Emit()
			wait(0.5)
			ball.CFrame = originalPosition.CFrame
			ball.Anchored = true
		end
		wait(1)
		ball.Anchored = false
		debounce = false
	end
end)

goalB.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		if hit == ball then
			Team2Value.Value = Team2Value.Value + 1
			ball.Hitbox.Particle.Score:Emit()
			wait(0.5)
			ball.CFrame = originalPosition.CFrame
			ball.Anchored = true
		end
		wait(1)
		ball.Anchored = false
		debounce = false
	end
end)

Okay, Ill see if this will work

It doesnt work unfortunately, what else should i do?

Try this change:

local goalB = workspace.GoalB
local goalA = workspace.GoalA
local Team1Value = game.ReplicatedStorage.Team1Value
local Team2Value = game.ReplicatedStorage.Team2Value
local ball = workspace.SoccerBall
local originalPosition = workspace.BallSpawn
local debounce = false

goalA.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		if hit:FindFirstAncestor("SoccerBall") then
			Team1Value.Value = Team1Value.Value + 1
			ball.Hitbox.Particle.Score:Emit()
			wait(0.5)
			ball.CFrame = originalPosition.CFrame
			ball.Anchored = true
		end
		wait(1)
		ball.Anchored = false
		debounce = false
	end
end)

goalB.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		if hit:FindFirstAncestor("SoccerBall") then
			Team2Value.Value = Team2Value.Value + 1
			ball.Hitbox.Particle.Score:Emit()
			wait(0.5)
			ball.CFrame = originalPosition.CFrame
			ball.Anchored = true
		end
		wait(1)
		ball.Anchored = false
		debounce = false
	end
end)

I fixed it already thanks for helping tho

Post your fix so others know how to solve it too please.

The ball was inside of another part, so i just changed the ball variable into that part

1 Like