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

How would I make a speed button using ContextActionService?

Asked by 4 years ago

I have been having a problem, I just started to learn how to use the ContextActionService and I made a speed button. The problem is, is that when you press the speed button it puts your speed to 32. But I wanna make it where when you press it again it sets your speed 16. Please help.

Code:

local cas = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local char = player.Character

function buttonPress()
    char.Humanoid.WalkSpeed = 32
end


local speedMobileButton = cas:BindAction("SpeedBtn",buttonPress,true,"d")
cas:SetPosition("SpeedBtn",UDim2.new(0.72,-25,0.20,-25))
cas:SetTitle("SpeedBtn", "Speed")

1 answer

Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
4 years ago
Edited 4 years ago

You would need to create a toggle, and doing so is extremely easy using a bool:

local cas = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local char = player.Character

local toggle = false
function buttonPress()
    if toggle then
        char.Humanoid.WalkSpeed = 32
        toggle = false
    else
        char.Humanoid.WalkSpeed = 16
        toggle = true
    end
end


local speedMobileButton = cas:BindAction("SpeedBtn",buttonPress,true,"d")
cas:SetPosition("SpeedBtn",UDim2.new(0.72,-25,0.20,-25))
cas:SetTitle("SpeedBtn", "Speed")

If you have anymore questions about this, then let me know, otherwise mark it as the accepted answer.

Ad

Answer this question