How to make my Imagebutton screen GUI turn on and off when clicked

Hi! I have recently made a Imagebutton based ScreenGUI, where when you click it, it changes the player walkspeed from the standard 16 studs/sec to 25 to make the player move faster, basically a button making the player go faster.

The localscript works perfectly fine and all, but I want to be able to script it so when you click it again it reverts back to the normal 16 walkspeed withouth having to use MouseButton1Click to change from 16 to 25 and then use MouseButton2Click. So basically to click it once and it changes walkspeed from standard 16 to 25, and then to be able to click again to change back from 25 to 16 and repeat etc.

My current GUI and localscript is located simple in StarterGUI like this:
image

This is my current script that just changes speed just from 16 to 25, but not back:

local players = game.Players.LocalPlayer
local runningspeed = 25

script.Parent.MouseButton1Click:Connect(function()
    players.Character.Humanoid.WalkSpeed = runningspeed
end)

Thanks for all your help! Just to make it clear that my aim is just to be able to change it from the standard walk speed to 25 like the current script above does, but then be able to click again and change it back to 16 and so on!

Much appreciated, DannyGT7

1 Like
local players = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait() -- get the player's character
local runningspeed = 25
local defaultRunningSpeed = 16 -- default roblox character speed

local isRunning = false -- boolean value to tell if the player is sprinting

script.Parent.MouseButton1Click:Connect(function()
    local humanoid = character.Humanoid -- set the player's humanoid as a variable

    if not isRunning then -- check if the player is running
       isRunning = true -- set the boolean to true
       humanoid.WalkSpeed = runningspeed
    else
       isRunning = false -- set the boolean to false
       humanoid.WalkSpeed = defaultRunningSpeed -- set the player's walkspeed to the default
    end
end)

although I recommend remote events because exploiters can spoof this

1 Like

Thank you so much! It works perfectly fine! Thanks so much for the help, it is much appreciated! Also thanks for the notice on remote events, will definitely use that advice!

1 Like