How to use a key to kick a soccer ball

Hello! I am currently trying to make a system which kicks a soccer ball whenever you press “e” I have been looking for a solution to this for a while, but I have not been able to find one. Does anyone know a way this is possible? I’ve tried using tweening to find a way to move it, but i still have not been able to do this successfully. I already have a piece of code to execute the movement, i just want to know how to do the movement.

    local plr = game.Players.LocalPlayer

plr:GetMouse().KeyDown:Connect(function(k)
	if k == "e" then
		--what do I put here?
		
	end
	
end)
2 Likes

Hi,

All you need to do is give the ball a velocity.
You can do this by changing the Velocity property of the part.

Ball.Velocity = Vector3.new(x,y,z)

You would ofcourse play around with x,y,z to get the velocity how you want it.

1 Like

How would I make it so that it would only change the velocity when the player is close to the ball?

I can only think of is If you are further away from the ball’s magnitude then the higher the distance the lower the velocity but lower the distance the more
velocity value of your kick
And tbh Idk how to do that :confused: sorry

You can always check how far away a player is from the ball

if (Player.Character.HumanoidRootPart.Position - Ball.Position).Magnitude < 5 then
    Ball.Velocity = Vector3.new(x,y,z)
end

This is what i have currently, Is there anything wrong? My sound won’t play in it doesnt look like the ball is moving.
Ball = game.Workspace.Part
local animationTrack = game.Workspace.Part.kick
local Player = game.Players.LocalPlayer
local sound = game.Workspace.Part.Kick

    local function Sound()
    	if not sound.IsPlaying then
    		sound.Play()
    	end
    	
    end



    game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
    	if inputObject.KeyCode == Enum.KeyCode.E then
    		if (Player.Character.HumanoidRootPart.Position - Ball.Position).Magnitude < 5 then
    			Ball.Velocity = Vector3.new(2,2,2)
    			animationTrack:Play()
    			Sound()
    			wait(0.1)
    			Ball.Velocity = Vector3.new(1,1,1)
    		end
    		
    	end
    end)
1 Like

Correct me if I’m wrong, but I’m not sure you can set velocity. What I would suggest is creating a BodyVelocity with your velocity and Destroy()ing it after about 1 second. Also, it looks like the ball would only move in a single direction with your provided script. I would suggest getting Player.Characrer.HumanoidRootPart.CFrame.LookVector*num where num is your desired velocity. That way, the ball will move the direction the player faces. Hope this helps!

2 Likes

You can set Velocity. No need to use a hacky approach.

3 Likes

I tested my script and nothing happens, so I do not know what the issue is atm.

1 Like

Hi,
sound:Play() should be used instead of sound.Play

As well, play around with the velocity, (try bigger values)

1 Like

Whenever I press e, nothing happens, and the sound still does not play. I have the sound under the part and the animation is there too, but doesnt happen either. The ball when touched just flies into one direction at high speeds

Is there an error you are getting?

Nope, not even getting an error. That’s why I assumed I did something wrong in the variables or used the wrong function

1 Like

My bad for not giving a better explanation to what I want, because currently the ball isn’t working. I want a GUI that is above the ball whenever you are close to it. The GUI will simply just have the letter E on it. Whenever you press E, the ball will move on itself, not on user impact. I also will implement a sound and an animation to go with it. If anyone has an idea on how I could fix this, please let me know!

Hi - even if you had the ball to be kicked, it would only work on the client. You need to use a RemoteEvent for it to go to the server.

There are multiple ways you can do this;

  • Changing the Velocity property of the ball manually based on the direction you intend to kick it in. Example: Ball.Velocity = Character["Right Leg"].CFrame.LookVector * 10 -- 10 is the power you prefer

  • You can make use of BodyVelocity instance that is parented to the ball for movement. You can adjust the properties similarly to the first point mentioned. You can combine this with using the service Debris, so you can destroy the instance after a prefered time. Example: game:GetService("Debris"):AddItem(Instance, Time)

These would give you the desired results making use of physics. However, if you attempt to do this on your client alone, you would need to be the NetworkOwner of the part. This would not work if you are not, unless the ball you created is through the client. So you would need to communicate with the server via the use of Remotes to make the changes needed to make it possible.

You should also consider using a hit box method such as: Touched, Magnitude, Region3, Raycast to register the hit and carry on the physics functions.

Also a correction to your Sound function needs to be made. It should be sound:Play() instead of sound.Play()

1 Like

You can try making it a handle. Make a script in ServerScript and add a module in it and have all the maths in the module. If you need me to script it for you I will

Would appreciate it, if you need any pay all I got it 100 robux at this moment, but I am willing to pay it if you could help!

If you trying to hire me you don’t gotta pay

But I only have something like that for basketball
But its the same thing