Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make a speed button using ContextActionService?

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

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.