Game sometimes not detecting part

so i have a ball and have a script that detects when the ball touches a part but after a few times the script sometimes doesnt detect the ball

local ball = script.Parent
local BlueGoal = workspace.BlueGoal
local RedGoal = workspace.RedGoal
local RoundTeleport = workspace.RoundTeleport
local blueScore = game.ReplicatedStorage.BlueScore
local redScore = game.ReplicatedStorage.RedScore

BlueGoal.Touched:Connect(function(hit)
	if hit == ball then
		ball.Anchored = true
		ball.Position = Vector3.new(60.366, 7.596, -16.019)
		ball.Velocity = Vector3.new()
		ball.AssemblyAngularVelocity = Vector3.new()
		ball.Transparency = 1
		ball.CanCollide = false
		redScore.Value += 1
		wait(5)
		ball.CanCollide = true
		ball.Transparency = 0
		ball.Anchored = false
	end
end)

RedGoal.Touched:Connect(function(hit)
	if hit == ball then
		ball.Anchored = true
		ball.Position = Vector3.new(60.366, 7.596, -16.019)
		ball.Velocity = Vector3.new()
		ball.AssemblyAngularVelocity = Vector3.new()
		ball.Transparency = 1
		ball.CanCollide = false
		blueScore.Value += 1
		wait(5)
		ball.CanCollide = true
		ball.Transparency = 0
		ball.Anchored = false
	end
end)

There could be several problems with your system

.Touched unreliability

.Touched events tend to be very inconsistent as they run every RunService.Heartbeat so if a part is moving faster than the server frame there is a chance it will seemingly phase through the part.

If you’re dedicated to learning how to better your scripts I would recommend making a raycast system for the ball to detect the goal so you no longer rely on physics but moreover a single line that can be any length you desire.
if you want I can help you out with this

CanTouch property staying disabled

There is also a chance that after you turn off the CanCollide property the CanTouch property also disables which makes it not fire .Touched events with other parts. Simply putting a ball.CanTouch = true line after the wait might fix your problem.

  • PS: it’s better practice and optimization to use task.wait(5) instead of wait(5)
1 Like

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