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.
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
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
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)