Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make base speed increase when holding down a button?

Asked by 3 years ago

So I am trying to making speed increase when you hold down a button, I was only able to make it increase when clicking a key again and again. This is what I have rn.

game.ReplicatedStorage.Takeoff.OnServerEvent:connect(function(client, event, data)
    if event == "On" and client == player then
        bv.maxForce = Vector3.new(math.huge, math.huge, math.huge)
        bg.maxTorque = Vector3.new(math.huge, math.huge, math.huge)
    elseif event == "Speed" and client == player then   
        baseSpeed = baseSpeed + 10
    elseif event == "Slow" and client == player then    
        baseSpeed = baseSpeed - 5
    elseif event == "Fly" and client == player then
        bg.CFrame = data
        direction = bg.CFrame.lookVector
        direction = Vector3.new(direction.X, direction.Y, direction.Z).unit
        bv.Velocity = direction*baseSpeed
    elseif event == "Off" and client == player then
        bv.maxForce = Vector3.new(0,0,0)
        bg.maxTorque = Vector3.new(0,0,0)
    end
end)a

2 answers

Log in to vote
0
Answered by 3 years ago

I don't quite get each variables used in the script. It's always good to give a background on the variables used, so we could determine what it does, how it triggers, or act.

I suggest creating a variable named "keyOnHold". On Gui.MouseButton1Down (if mouse is held down), set keyOnHold to true. Then put everything on a loop that goes on while keyOnHold is true.

On Gui.MouseButton1Up (if mouse is released), set keyOnHold to false. That will stop the loop.

*Note that Gui.MouseButton1Down checks if the button is held down. It is different from Gui.MouseButton1Click, which checks if the button was both held down AND released (simply, clicked).

local keyOnHold = false
local button = Gui.Button

while keyOnHold do
     --Add code on manipulating speed
end

button.MouseButton1Down:Connect(function() keyOnHold = true end) 
button.MouseButton1Up:Connect(function() keyOnHold = false end)

Ad
Log in to vote
0
Answered by
DollorLua 235 Moderation Voter
3 years ago

User input service has a function called "IsKeyDown" (im gonna reference user input service as uis)

here is some example code that might get you on the right path.

local delay = 0.1
local speed = 0

while wait(delay) do -- I would usually use a custom wait function here
    if uis:IsKeyDown(Enum.KeyCode.W) then -- IsKeyDown takes one parameter, a keycode
        speed += 1
    else
        speed = 0 -- this means that they are no longer holding the key, so lets reset the value.
    end
end

Answer this question