Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would I multiply or divide the Humanoid WalkSpeed?

Asked by
Scaii_0 145
7 years ago

I have a working shift to sprint script and it sets the sprint speed to a certain digit when sift is pressed and sets it back to 16 when let go of.

But the problem is that it is colliding with one of my other script where the player is not allowed to move for a while: script.Parent.Touched....ect local h = ...Humanoid h.JumpPower = 0 h.WalkSpeed = 0

But you can just get out of this by clicking shift.

My sprint script is:

local mouse = game.Players.LocalPlayer:GetMouse() local running = false local S = script.Parent.Parent.Character.Humanoid.Walkspeed

function getTool()
for _, kid in ipairs(script.Parent:GetChildren()) do if kid.className == "Tool" then return kid end end return nil end

mouse.KeyDown:connect(function (key) -- Run function key = string.lower(key) if string.byte(key) == 48 then running = true local keyConnection = mouse.KeyUp:connect(function (key) if string.byte(key) == 48 then running = false end end) for i = 1,5 do game.Workspace.CurrentCamera.FieldOfView = (70+(i2)) wait() end game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 24 --I want to multiply here repeat wait () until running == false keyConnection:disconnect() game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 --And divide here for i = 1,5 do game.Workspace.CurrentCamera.FieldOfView = (80-(i2)) wait() end end end)

Can anyone help me with this. I've tried using a local value of S and multiplying that but it doesn't seem to work.

0
Use code block, I can't read your code BlackOrange3343 2676 — 7y

3 answers

Log in to vote
0
Answered by 6 years ago

To answer your question, performing arithmetic on a humanoid's speed is as simple as referencing it. No additional variables are required, unless you feel inclined to implement them. Also, remember to use code blocks when you post scripts in your questions. They make it easier for us to read your code.

To add to that, I would advise that you switch to UserInputService.

In my opinion, utilizing UserInputService is a better solution when you are trying to manipulate keyboard input. :KeyDown is a deprecated event, meaning it should not be used for future work. UserInputService consists of two basic events. :InputBegan and :InputEnded. These two events are used to detect the start and end of a specific input, just like :KeyDown and :KeyUp. Here is an example of a sprint script using UserInputService. Like you suggested, the humanoid's speed will be multiplied and divided by a specific value.

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Multiplier = 2

UIS.InputBegan:Connect(function(Input, Game)
if Game == true then return end
if Input.KeyCode == Enum.KeyCode.LeftShift then
Humanoid.WalkSpeed = Humanoid.WalkSpeed * Multiplier
end
end)

UIS.InputEnded:Connect(function(Input, Game)
if Game == true then return end
if Input.KeyCode == Enum.KeyCode.LeftShift then
Humanoid.WalkSpeed = Humanoid.WalkSpeed / Multiplier
end
end)

The effect of sprinting is always proportional to the humanoid's current speed, so an external freeze script should not interfere with the functionality of this one. You can change the multiplier as you wish.

Good luck! - Preston

0
I didn't realize how ancient this question was. Hopefully this answer remains helpful to present viewers. To anyone reading this, have a good day/night. ThatPreston 354 — 6y
Ad
Log in to vote
1
Answered by 7 years ago

In Lua for multiplication you use the asterisk character (*) and for division you use the forward slash character (/)

Example if you wanted to multiply the walkspeed by 2:

game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed * 2
0
I tried this it did not work... Scaii_0 145 — 7y
0
Lol. You think all you need is that line to make it run? Think about where you are calling your code. You should also be debugging with print statements to the console. Impacthills 223 — 7y
Log in to vote
0
Answered by
iRexBot 147
7 years ago

We can try doing this by saving the WalkSpeed as a number value.

local walkString = tostring(playerHumanoid.WalkSpeed .. " ")
local walkSpeed = tonumber(walkString)
walkSpeed = walkSpeed/amount
walkSpeed = walkSpeed*amount

(I have not tested this I can help you more if any bugs are found)

Answer this question