Run/Walk script not running

Hello, I am making a run/walk script using only one button.

I am using a localscript inside of a textbutton in startergui. I have used print statements and non of them run which indicated the script is not even running.

Script:

local players = game:GetService("Players")

local player = players.LocalPlayer
local character = player.CharacterAdded:Wait()
local humanoid = character.Humanoid

local runButton = script.Parent

local isRunning = false

runButton.MouseButton1Click:Connect(function()
	if not isRunning then
		isRunning = true
		humanoid.WalkSpeed = 64
		runButton.Text.TextColor = Color3.new(0, 1, 0)
	else
		isRunning = false
		humanoid.WalkSpeed = 16
		runButton.Text.TextColor = Color3.new(1, 0, 0)
	end
end)

Properties hierarchy:

image

Help would be appreciated :slightly_smiling_face:

1 Like

So basically what you wanna do is press a button to make the player the run, then again for the player to walk etc. Like press to run, the press again to walk then repeat vice versa

1 Like

That is exactly my aim. I also want the button to turn green if they are running and red if they are not.

1 Like

Try this, should work I hope. Just adjust it to your specific needs and requirements like the pathway to the location of the GUI button and stuff:

local players = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait() -- get the player's character
local runningspeed = 25 --Change this to however much you want the running speed to be
local defaultRunningSpeed = 16 -- default roblox character speed

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

script.Parent.MouseButton1Click:Connect(function() --Change the pathway and amount of parents to however much you need
    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 or change to however much you want
    end
end)
2 Likes

Try this:

script.Parent.MouseButton1Click:Connect(function(player)
     local char = player.Character
     local humanoid = player:FindFirstChild("Humanoid")
     
     if humanoid.WalkSpeed == 16 then
            humanoid.WalkSpeed = 64
     else
            humanoid.WalkSpeed = 16
      end
end)

This event doesn’t have a player parameter.

Api:


No signs of a parameter.

1 Like

Sorry, forgot to include colors. This should work, works perfectly fine on mine. Adjust whatever u need to ur specific needs. Here you go again lol my bad:

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
	    runButton.Text.Textcolor = Color3.fromHSV(0, 1, 1)
    else
	    isRunning = false -- set the boolean to false
	    humanoid.WalkSpeed = defaultRunningSpeed -- set the player's walkspeed to the default
	    runButton.Text.Textcolor = Color3.fromHSV(0.333333, 1, 1)
    end
end)

Why would it need a player parameter if I already have a player variable?

Exactly, it’s unnecessary for it to have a player parameter when you have LocalPlayer.

1 Like

Oh I thought you was talking about my script, sorry.

1 Like

But it is a TextButton so it has that perimeter.