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
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 :)