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

How do I make a button toggle feature?

Asked by 4 years ago

How do I make a button toggle some piece of code? and then when pressed again toggle another piece of code. I want to make a sprint feature which will be toggled by the press of the button q. This is my attempt.

Username = LocalPlayer
local on = false
local sprinting = true
if q is pressed then
on = true
sprinting = true
else

end

if sprinting = true then
game.Workspace.Username.Humanoid.WalkSpeed = speed
end

1 answer

Log in to vote
0
Answered by
AspectW 431 Moderation Voter
4 years ago
Edited 4 years ago
local UserInputService = game:GetService("UserInputService")
local running = false

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end --if typing in a gui object then end
    if input.KeyCode == Enum.KeyCode.Q then --if pressed Q then
        if running then --if is already running
            --your code to make player stop running here
            running = false --tell the script the player is no longer running
        else --if not running then
            --your code to make the player run here
            running = true --tell the script the player is running
        end
    end
end)

Basically, a variable that we switch "on/off". I recommend you to read this article on UserInputService: https://developer.roblox.com/en-us/api-reference/class/UserInputService If you have further questions, feel free to drop a comment on this answer :)

0
Thank you! It worked flawlessly! UgllyBoyCult 2 — 4y
Ad

Answer this question