Move ball when clicked?

I’m trying to figure out how to fire a ball forward when clicked. It’s not going to a set position, I just want it to fire forward when clicked. What I have so far but doesn’t move right now -

local part = script.Parent 
local clickDetector = script.Parent:FindFirstChild("ClickDetector") 


local function onClick()
	part:MoveTo(part.Position + Vector3.new(30, 0, 0)) 
end

clickDetector.MouseClick:Connect(onClick)

1 Like

MoveTo is a method from the Humanoid Class (else it is a humanoid)

1 Like
local part = script.Parent 
local clickDetector = script.Parent:FindFirstChild("ClickDetector") 

local function onClick()
	part.CFrame = part.CFrame * CFrame.new(30, 0, 0)
end

clickDetector.MouseClick:Connect(onClick)
1 Like

This does work but the ball just moves from one set position to another. I just want it to fire forward at a certain speed so it bounces around randomly.

1 Like

Then you might want to use TweenService so it slides smoothly like this:

local part = script.Parent 
local clickDetector = script.Parent:FindFirstChild("ClickDetector") 
local tweeninfo = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)

local function onClick()
	local tween = game:GetService("TweenService"):Create(part,tweeninfo,{["Position"]=part.Position+Vector3.new(0,0,30)})
	tween:Play()
end

clickDetector.MouseClick:Connect(onClick)

I think I might just use a ramp instead. Thanks for the suggestions anyway guys.

To make the ball go forward smoothly you can use BodyAngularVelocity.
What I did is: Create a Ball with 8, 8, 8 of size.and insert a ClickDetector and a Script with the following code:

local Ball = script.Parent
local ClickDetector = Ball.ClickDetector
local Debris = game:GetService("Debris")

local Force = Ball:GetMass() * 750 -- You can change 750 

function onMouseClick()
	local BodyAV = Instance.new("BodyAngularVelocity", Ball)
	BodyAV.AngularVelocity = Ball.CFrame.LookVector * game.Workspace.Gravity * Force
	Debris:AddItem(BodyAV, 0.5)
end

ClickDetector.MouseClick:Connect(onMouseClick)

I already tested it and it works.

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