Hello, here I am programming a football game, except that I do not have a great experience in the script on roblox, and I would like to be able to make a script which makes that when my character touches the ball, and well he can move all over the land, i tried to make an animation just to have fun
You could use vector forces, here’s the documentation, and you can get the direction from the player to the ball and “kick” the ball in that direction by applying a vector force.
To get direction from the player to the ball:
local playerCharacter = --your players character
local ball = --ball
local direction = (playerCharacter.HumanoidRootPart.Position - bal..Position).Magnitude
direction = Vector3.new(direction.x, 0, direction.z) -- this will shoot the ball flat
--if you want to shoot the ball upwards too, change the Y (0) to a bigger number
Can you give me an example of what this looks like for you, can you give me a version of the script with the path indicated where to place the script + the filled script? instead of --word-- to make sure I’m not mistaken?
I believe this may work:
(put in ball as a server script)
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local playerCharacter = hit.Parent
local ball = script.Parent
local direction = (playerCharacter.HumanoidRootPart.Position - ball.Position).Magnitude
direction = Vector3.new(direction.x, 0, direction.z)
local att = Instance.new("Attachment")
att.Parent = ball
local force = Instance.new("VectorForce")
force.Parent = ball
force.Attachment0 = att
force.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
force.ApplyAtCenterOfMass = true
force.Force = direction
end
end)
Yeah, this will work, but right now the direction is a value between 1 and 0, so it will be very small, and might not even move the ball. So I suggest you multiply it like this:
-- put this BEFORE "local force = ..."
local shootPower = 10
--and then at the end:
force.Force = direction * shootPower
Right now the shootPower is 10, which I think is still too small to move the part, so you will have to experiment with it to get the right value, or maybe randomize it so it won’t shoot the same power every time, or increase it based on the players shoot power stats or something like that.
Full script:
local ball = script.Parent
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local playerCharacter = hit.Parent
local direction = (playerCharacter.HumanoidRootPart.Position - ball.Position).Magnitude
direction = Vector3.new(direction.x, 0, direction.z)
local att = Instance.new("Attachment")
att.Parent = ball
local shootPower = 10
local force = Instance.new("VectorForce")
force.Parent = ball
force.Attachment0 = att
force.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
force.ApplyAtCenterOfMass = true
force.Force = direction * shootPower
end
end)
One thing you have to note is that the VectorForce will apply a constant force to the ball. So if you don’t destroy it, it will make the ball accelerate for eternity. Look into this article for an alternate way (ApplyImpulse).
local ball = script.Parent
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local playerCharacter = hit.Parent
local direction = (playerCharacter.HumanoidRootPart.Position - ball.Position).Magnitude
direction = Vector3.new(direction.x, 0, direction.z)
local shootPower = 10
ball:ApplyImpulse(direction * shootPower) -- this wont move the ball continuously
end
end)