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
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
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.
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:
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