Touch event not working

I am trying to make a football game want to make a football move up when it is touched, but it seems I can not even get down the basic touch event.

This is my script:

local hitbox = script.Parent
local ball = hitbox.Parent

while true do
	wait()
	hitbox.Position = ball.Position
end

hitbox.Touched:Connect(function(hit)
	print("touch")
	if hit.Parent:FindFirstChild("Humanoid") then
		local hum = hit.Parent:FindFirstChild("Humanoid")
		local hrp = hum.Parent:FindFirstChild("HumanoidRootPart")
		local cf = hrp.CFrame
		local lookvector = cf.LookVector

		ball.AssemblyLinearVelocity = lookvector
	end
end)

And this is where it is located
2024-03-30
Can touch is turned on for the hitbox and the hitbox is anchored

I have looked for multiple other forms with people with my same problem but I can not find the right solution, I have tried moving the script from the mesh to the hitbox but it will not work. Help will be appreciated.

local hitbox = script.Parent
local ball = hitbox.Parent

while true do
wait()
hitbox.Position = ball.Position
end

hitbox.Touched:Connect(function(hit)
print(“touch”)
local hum = hit.Parent:FindFirstChild(“Humanoid”)
If hum then
local hrp = hum.Parent:FindFirstChild(“HumanoidRootPart”)
local cf = hrp.CFrame
local lookvector = cf.LookVector

	ball.AssemblyLinearVelocity = lookvector
end

end)

Get rid of if hit, change to local hum =

Then add…. if hum then

No, the print(“touch”) is not working. That is my problem.

If hit then
Print (touched)

See if that fixes it

The code after the while true loop cannot run until the while true loop is broken, instead of using the loop, just create a weld between the football and the hitbox, then unanchor the hitbox.

The issue is you’re using:

while true do
wait()
hitbox.Position = ball.Position
end

This is ALWAYS set to true, meaning this code will repeat endlessly.

I have 2 solutions for you:

  1. Use a coroutine:
local hitbox = script.Parent
local ball = hitbox.Parent

coroutine.wrap(function()
	task.wait()
	hitbox.Position = ball.Position
	print("Moved")
end)()


hitbox.Touched:Connect(function(hit)
	print("touch")
	if hit.Parent:FindFirstChild("Humanoid") then
		local hum = hit.Parent:FindFirstChild("Humanoid")
		local hrp = hum.Parent:FindFirstChild("HumanoidRootPart")
		local cf = hrp.CFrame
		local lookvector = cf.LookVector

		ball.AssemblyLinearVelocity = lookvector
	end
end)

or use .Heartbeat:

local hitbox = script.Parent
local ball = hitbox.Parent
local RS = game:GetService("RunService")

RS.Heartbeat:Connect(function()
	task.wait()
	hitbox.Position = ball.Position
end)

hitbox.Touched:Connect(function(hit)
	print("touch")
	if hit.Parent:FindFirstChild("Humanoid") then
		local hum = hit.Parent:FindFirstChild("Humanoid")
		local hrp = hum.Parent:FindFirstChild("HumanoidRootPart")
		local cf = hrp.CFrame
		local lookvector = cf.LookVector

		ball.AssemblyLinearVelocity = lookvector
	end
end)

Both of these work, however one thing that doesn’t is the ball being “launched”

I’ve never really worked with LinearVelocity, but I think you’re meant to use VectorForce.

Correct me if I’m wrong though.

Edit: or do what @STK_gold1 said, their solution is simpler

The second piece of code worked, thank you very much.

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