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

How do I make a button toggle feature?

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

✓ Accepted answer
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 :)

1 comment

0

Thank you! It worked flawlessly!

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