Help to script walkspeed

the local player presses the button and he has an initial speed, and when he presses back, the speed of his returns I spent a lot of time and could not do it, I am making a game similar to a super power fighting game simulator and when I made a script and pressed the button, I was reset, then when I got the speed, it did not work

5 Likes

example:

I’m new to scripting

3 Likes

so upon button press walkspeed up?

2 Likes

No, it doesn’t increase, it becomes 16 speed

2 Likes

When a local player presses the button, his speed becomes 16, and when pressed again, his speed returns. But the problem is that when I press the button, the speed becomes 16, but when I run, the speed returns

3 Likes

to change a value of WalkSpeed, you can interact with Humanoid that has this property and change default speed to new one (by default, Roblox set the speed to 16)

changing WalkSpeed will be much easily thing to do:

-- Interact with Players Service.
local PService = game:GetService("Players")

-- Creating a table that will contain player and character objects. This thing will be much useful to you.
local User = {
	Player = PService.LocalPlayer,
	Character = PService.LocalPlayer.Character or PService.LocalPlayer.CharacterAdded:Wait(),
	Humanoid = PService.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
}

-- Creating variables which first one contains default old speed (16) and new one that contains new speed (e.g 32).
local OldSpeed = User.Humanoid.WalkSpeed -- This will return number 16 so don't get confused!
local NewSpeed = 32 -- Our NewSpeed.

-- Creating a function with UserInputService that will detect key and call a function.
local UIS = game:GetService("UserInputService")

local toggle = false -- That is for toggle.

UIS.InputBegan:Connect(function(input, gpe) -- This event will get detected whatever input has began.
	if gpe then -- GPE will set to true if any core game object were interacted (e.g someone typed in the chat).
		return -- If GPE were true then we will exit the function.
	end
	if input.KeyCode == Enum.KeyCode.Q then
		if not toggle then -- Check if toggle is false.
			toggle = not toggle -- Reverse toggle's false to true.
			User.Humanoid.WalkSpeed = NewSpeed -- Returning WalkSpeed property to NewSpeed.
		elseif toggle then -- Check if toggle is true.
			toggle = not toggle -- Reverse toggle's true to false.
			User.Humanoid.WalkSpeed = OldSpeed -- Returning WalkSpeed property to OldSpeed.
		end
	end
end)

you may need to try this one, however, we don’t know which script are you working on

4 Likes

Sorry again thanks for the script, but I have it, I work so that when the local player presses the button, his speed is reset, and when he presses, it returns

1 Like

I have a problem that the player resets when pressed and returns after 1 second

1 Like

could you please provide your code which you’re currently working on

1 Like

An example from this video

1 Like

But that’s not what I’m doing, I’m doing it so that the local player presses the button that is in the startergui

1 Like

oh, well, you can make an TextButton or ImageButton in one ScreenGUI and check if it has been pressed through MouseButton1Click or TouchTap event, depending on Player’s device

so, changing my code will look a bit different:

-- Interact with Players Service.
local PService = game:GetService("Players")

-- Creating a table that will contain player and character objects. This thing will be much useful to you.
local User = {
	Player = PService.LocalPlayer,
	Character = PService.LocalPlayer.Character or PService.LocalPlayer.CharacterAdded:Wait(),
	Humanoid = PService.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
}

-- Creating variables which first one contains default old speed (16) and new one that contains new speed (e.g 32).
local OldSpeed = User.Humanoid.WalkSpeed -- This will return number 16 so don't get confused!
local NewSpeed = 32 -- Our NewSpeed.

-- nvm about this (Creating a function with UserInputService that will detect key and call a function.)
local UIButton = script.Parent -- My local script were located in my TextButton, so you may need to change your directory of an UIButton

local toggle = false -- That is for toggle.

UIButton.MouseButton1Click:Connect(function() -- This event will be detected if UIButton has been pressed.
	if not toggle then -- Check if toggle is false.
		toggle = not toggle -- Reverse toggle's false to true.
		User.Humanoid.WalkSpeed = NewSpeed -- Returning WalkSpeed property to NewSpeed.
	elseif toggle then -- Check if toggle is true.
		toggle = not toggle -- Reverse toggle's true to false.
		User.Humanoid.WalkSpeed = OldSpeed -- Returning WalkSpeed property to OldSpeed.
	end
end)

not working your script

18:52:49.111 Players.Lamino961.PlayerGui.ScreenGui.TextButton.LocalScript:12: attempt to index nil with ‘WalkSpeed’ - Client - LocalScript:12

1 Like

Your script is resetting the db variable, So it is only a one way script …

local part = script.Parent
local norm, fast = 16, 32
local db = false

local function onButtonClicked(player)
	if not db then db = true
		workspace:FindFirstChild(player.Name).Humanoid.WalkSpeed = fast
		part.BrickColor = BrickColor.new("Bright red")
	else db = false
		workspace:FindFirstChild(player.Name).Humanoid.WalkSpeed = norm
		part.BrickColor = BrickColor.new("Lime green")
	end
end

part.ClickDetector.MouseClick:Connect(function(player)
	onButtonClicked(player)
end)

You can add the script here as I did using the </> option when posting.

1 Like

I need a local script a local player presses a button and his speed becomes 16 and when he presses the button again, his speed returns to what he had

I’m just showing you how to build a toggle script. Apply that logic to whatever.
See how db is controlling the script flow and not just acting like a flag.

ok ths for information I’m new to scripting

This may be easier to understand …

local function onButtonClicked(player)
	if db then db = false
		workspace:FindFirstChild(player.Name).Humanoid.WalkSpeed = norm
		part.BrickColor = BrickColor.new("Lime green")
	else db = true
		workspace:FindFirstChild(player.Name).Humanoid.WalkSpeed = fast
		part.BrickColor = BrickColor.new("Bright red")
	end
end

Kind of guessing here … should work all local from the one script.

sorry this is junk … we need to NOT use a local script for this …

bro your script not working I checked all the methods, but not one does not work, EVA probably played yes in a super power fighting simulator and you saw that when you pump up the speed, you start running fast, so when the player presses the button and he starts running at 16 speeds, and when he presses again, his speed becomes the same, that is, it returns