I'm trying to make the player go faster and play an animation while a certain key is being pressed but nothings working.
this is the code I put in the starter pack so far:
local playr = game.Players.LocalPlayer local mouse = playr:GetMouse() local uis = game:GetService("UserInputService") local character = playr.CharacterAdded:wait(0.1) character:WaitForChild("Humanoid") character.Humanoid.WalkSpeed = "50" local animationTrack = playr.Character.Humanoid:LoadAnimation(animation) function run(key,gp) if key.KeyCode == Enum.KeyCode.Q and gp == false then script.Fwoosh:Play() game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = "250" end end uis.InputBegan:connect(run) function dont(key) if key.KeyCode == Enum.KeyCode.Q and gp == false then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = "50" end end uis.InputEnded:connect(dont)
doesnt work :(
The error is that you are using “250” instead of 250, the walk speed cannot be a string, but not only that, you are not specifying the key when they press, so any key will make the player sprint, but you need to specify an input function for user input service otherwise the script will never know the key they clicked
local playr = game.Players.LocalPlayer local mouse = playr:GetMouse() local uis = game:GetService("UserInputService") local character = playr.CharacterAdded:Wait() -- Remove :wait(0.1) character:WaitForChild("Humanoid") character.Humanoid.WalkSpeed = 50 -- Use Numbers local animationTrack = playr.Character.Humanoid:LoadAnimation(animation) function run(key,gp) if key.KeyCode == Enum.KeyCode.Q and not gp then script.Fwoosh:Play() game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 250 end end function stop() game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 50 end uis.InputBegan:connect( function(input) if input.KeyCode == Enum.KeyCode.Q then run(Q) -- you have to tell the script the key the player clicked end end uis.InputEnded:connect( stop() --no point of the key parameter since they will let go of the key ) )
Answer
Well, first, you can put in StarterPlayer
considering it always updates your position even when the position is modified by a LocalScript
. So something like this may work:
local uis = game:GetService("UserInputService") local character = workspace:WaitForChild(script.Parent.Parent.Name) local sprinting = false --Camera options local EasingSpeed = 3 local sprintFOV = 120 local defaultFOV = 70 --Speed options local defaultRunSpeed = 16 local sprintRunSpeed = 32 --Jump options (optional) local highJump = true local defaultJumpPower = 50 local sprintJumpPower = 75 uis.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.LeftShift then sprinting = true end end) uis.InputEnded:Connect(function(input) if input.KeyCode == Enum.KeyCode.LeftShift then sprinting = false end end) while wait() do if sprinting == true then if workspace.CurrentCamera.FieldOfView < sprintFOV + 1 then workspace.CurrentCamera.FieldOfView = workspace.CurrentCamera.FieldOfView + EasingSpeed end character.Humanoid.WalkSpeed = sprintRunSpeed if highJump == true then character.Humanoid.JumpPower = sprintJumpPower end elseif sprinting == false then if workspace.CurrentCamera.FieldOfView > defaultFOV - 1 then workspace.CurrentCamera.FieldOfView = workspace.CurrentCamera.FieldOfView - EasingSpeed end character.Humanoid.WalkSpeed = defaultRunSpeed character.Humanoid.JumpPower = defaultJumpPower end end
If you need some things explained, continue to the next part.
Explanation
So, if you look at the script, you can see many variables at the top. What these really mean is simple:
EasingSpeed
= The amount of FOV (Field of View) it changes every tick when sprinting
sprintFOV
= The FOV that is eased to when sprinting
defaultFOV
= The default FOV (FOV when not sprinting)
defaultRunSpeed
= The Humanoid
s WalkSpeed
when not sprinting
sprintingRunSpeed
= The Humanoid
s WalkSpeed
when sprinting
highJump
= whether or not the Humanoid
s JumpPower
is affected by sprinting
defaultJumpPower
= the Humanoid
s JumpPower
when not sprinting
sprintingJumpPower
= the Humanoid
s JumpPower
when sprinting
Feel free to customize these how you wish!
flunkywhat, Game Dev