I have a issue with a script where if you hold shift at all it plays the run animation even if you aren’t running. It is really bugging me and a friend right now and we wanna fix it. Is there a way that I can see if someone is running and if they are the animation plays and their walk speed increases? Also another thing I wanna point out is if you jump while running the animation stops too and the normal run starts. (I am more of a builder than scripter btw)
Here’s my script:
local UIS = game:GetService(‘UserInputService’)
local Player = game.Players.LocalPlayer
local Character = Player.Character
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 24
local Anim = Instance.new(‘Animation’)
Anim.AnimationId = ‘rbxassetid://5837250013’
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
end
end)
UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 16
PlayAnim:Stop()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end)
You didn’t address PlayAnim, please put your code between 3 Ticks ( ``` ), and, format your code if possible. It makes it easier for at least, me, to understand.
try using :GetMoveVector() from the control module.
in your InputBegans if statement, i added this
controls:GetMoveVector() ~= Vector3.new(0,0,0)
local playerModule = require(player.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playerModule:GetControls()
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local PlayAnim
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift and controls:GetMoveVector()~=Vector3.new(0,0,0) then
Character.Humanoid.WalkSpeed = 24
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://5837250013"
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
end
end)
UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 16
if(PlayAnim)then
PlayAnim:Stop()
end
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end)
Hi, I noticed a few things about your version of the code that I would like to point out:
You are using :connect() instead of the new (it’s not even new anymore) :Connect(). (I know that’s how the code came, but you can always fix the little things)
When playing anims with key inputs, you always want to have some sort of check to make sure the player isn’t typing because it could activate the animation at the wrong time if not implemented.
Here is the re-finalized version:
local playerModule = require(player.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playerModule:GetControls()
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local PlayAnim
UIS.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed then
if input.KeyCode == Enum.KeyCode.LeftShift and controls:GetMoveVector()~=Vector3.new(0,0,0) then
Character.Humanoid.WalkSpeed = 24
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://5837250013"
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
for i = 1,5 do
workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
end
end
end)
UIS.InputEnded:Connect(function(input, gameProcessed)
if not gameProcessed then
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 16
if(PlayAnim)then
PlayAnim:Stop()
end
for i = 1,5 do
workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end
end)
you know gameProcessedEvent isn’t just for typing and he wasn’t asking for help on :connect information or how InputBegan is actually used, but yes your right.
Updated Code
local playerModule = require(player.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playerModule:GetControls()
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local PlayAnim
UIS.InputBegan:Connect(function(input, g)
if g then return end
if input.KeyCode == Enum.KeyCode.LeftShift and controls:GetMoveVector()~=Vector3.new(0,0,0) then
Humanoid.WalkSpeed = 24
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://5837250013"
PlayAnim = Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
end
end)
UIS.InputEnded:Connect(function(input, g)
if g then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
Humanoid.WalkSpeed = 16
if(PlayAnim)then
PlayAnim:Stop()
end
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end)
but since we’re going that far, lets go ahead and make sure the character and humanoid exist before we even allow them to use the input.
local Player = game.Players.LocalPlayer
local Character = Player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
Does all this code just go in the same script? I have my code in a local script in StarterCharacterScripts. I am a little confused about everything you guys are saying cause Idk much about roblox scripting.
21:08:47.822 Workspace.Zvtu.RunningScript:1: attempt to index nil with ‘PlayerScripts’ - Client - RunningScript:1
&
Requested module experienced an error while loading - Studio
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local playerModule = player:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")
local controls = require(playerModule:WaitForChild("ControlModule"))
local Character = Player.Character
local PlayAnim
the player wasn’t referenced yet when it tried to get the Player/Control module.
This code when placed inside a LocalScript in StarterPlayer in StarterCharacterScripts
local UIS = game:GetService("UserInputService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local Enable = false
local Ninja = Instance.new("Animation")
Ninja.Name = "Ninja"
Ninja.AnimationId = "http://www.roblox.com/asset/?id=656118852"
Ninja.Parent = script
humanoid.AnimationPlayed:Connect(function(animationTrack)
if animationTrack.Name == "RunAnim" and Enable == true then
Ninja = humanoid:LoadAnimation(script.Ninja)
Ninja:Play()
end
end)
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Enable = true
humanoid.WalkSpeed = 24
Ninja = humanoid:LoadAnimation(script.Ninja)
Ninja:Play()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
end
end)
UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Enable = false
humanoid.WalkSpeed = 16
Ninja:Stop()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end)