Creating a ball that the longer you hold your mouse button the faster it goes

I am relatively new with Lua, only been working with it for about a month but am very familiar with other programming languages such as java. I am trying to create a ball that the longer it is held down the faster it will go. I am confused on what should I use to make the ball move forward. Should I use CFrame, tweening, velocity, bodyforce? So far I have tried using CFrame and Bodyforce, but I can not seem to get them to work correctly since it moves the user backward instead of the ball forward.

1 Like

This post you made lacks severe details, such as how do you plan on it moving in the first place. Anyways, let me take you step by step. Disclaimer: The script is extremely basic and easy to make

First you would need the ball to move forward. Just as an example, we will use CFrames for this exercise.

local part = workspace.Part
local speed = 2
while wait(.5) do
    part.CFrame = part.CFrame + Vector3.new(0,0,2)
end

Every half a second, this script will add onto the CFrame to the part, giving it the feel of moving. Vector3.new takes 3 arguments, (x,y,z), and is used for positioning.

Now that we got that out of the way, let’s cover how to get Input from the user. The UserInputService is a service used to detect and capture the different types of input available on a user’s device. We start off by initializing the value. (Script belongs in StarterPlayerScripts)
`

local UIS = game:GetService(“UserInputService”)

`
Next, you call an event for whenever the User submits input. This event has 2 parameters, the key itself, and something known as gameProcessed, which allows us to detect if they are typing in chat or not. The following is the new code:

local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(key, gameProc)
    if not gameProc then
        --code
    end
end)

Finally, we use the key parameter to good use to detect what key is being pressed, and raise the speed if true. Here is our updated code:

local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(key, gameProc)
    if not gameProc then
        if key.KeyCode == Enum.KeyCode.K then --If the user presses "K"
            speed = speed + 2 --Raises the speed by 2 Stages
        end
    end
end)

There is only one problem with this code. This is a local script, and unless we write this in the same script as the initial script. It will not work. What we need to do is make the movement script relative. This means that the value would adjust accordingly and not be set in stone. Here is the updated server script.

local part = workspace.Part
local speed
while wait(.5) do
       if script:FindFirstChild("Speed") then
        speed = script.Speed --There is an IntValue inside the script named "Speed"
       else
           print("Speed Does Not Exist!")
    part.CFrame = part.CFrame + Vector3.new(0,0,2)
end

Perfect. Insert an IntValue inside the script and set the value to the number you want the speed to be. That being said, if this value was to ever change, the speed would change aswell. The last thing we would need to do is update the localscript to change the value of “Speed” (as an IntValue).

local UIS = game:GetService("UserInputService")
local script = game:GetService("ServerScriptService"):WaitForChild("Script") -- "Script" or whatever your script is named
local Speed = script:WaitForChild("Speed")
UIS.InputBegan:Connect(function(key, gameProc)
    if not gameProc then
        if key.KeyCode == Enum.KeyCode.K then --If the user presses "K"
            Speed.Value = Speed.Value + 2 --raises speed by 2 stages
        end
    end
end)

And now your script is done :slight_smile:

2 Likes

Thank you for replying, this was by first post in the forum. I will be sure to add more details in the future. I do understand that the script definitely isn’t that difficult to make but I just wasn’t fully sure which method to use.