I’ve made this code where it plays an different walking animation when the player is in shiftlock, and plays only the forward animation when the player isn’t in shiftlock.
The shitlock: on part works correctly, but the shitlock: off part has an issue, when I stop walking the animation just doesn’t stop playing, it only does this when I’m pressing A, S or D, and I don’t understand where I can fix this.
I hope my code is understandable, I’m new to scripting
Here’s my code:
-- Get services
local uis = game:GetService("UserInputService")
local Players = game:GetService("Players")
-- Variables
local Player = Players.LocalPlayer
Player.CharacterAdded:Wait()
local Character = Player.Character
local hum = Character:WaitForChild("Humanoid")
Player.Character:WaitForChild("Animate").Enabled = false
-- Animations
local forwardAnim = script:WaitForChild("ForwardAnimation")
local backwardAnim = script:WaitForChild("BackwardsAnimation")
local leftAnim = script:WaitForChild("LeftAnimation")
local rightAnim = script:WaitForChild("RightAnimation")
-- Tracks
local forwardTrack = hum:LoadAnimation(forwardAnim)
local backwardTrack = hum:LoadAnimation(backwardAnim)
local leftTrack = hum:LoadAnimation(leftAnim)
local rightTrack = hum:LoadAnimation(rightAnim)
-- Different keys to different tracks
local Keys = {
[Enum.KeyCode.W] = forwardTrack,
[Enum.KeyCode.S] = backwardTrack,
[Enum.KeyCode.A] = leftTrack,
[Enum.KeyCode.D] = rightTrack
}
-- Functions
-- Stops all currently running animations
function StopAll()
for _,v in pairs(Keys) do
v:Stop()
end
end
-- Detect player input and
uis.InputBegan:Connect(function(input, isTyping)
local KeyCode = input.KeyCode
-- Shiftlock on part
if uis.MouseBehavior == Enum.MouseBehavior.LockCenter then
if Keys[KeyCode] and not isTyping then
StopAll();Keys[KeyCode]:Play()
end
print("Playing animation: Shiftlock ON")
-- Shiftlock off part
else
if Keys[KeyCode] and not isTyping then
StopAll();forwardTrack:Play()
end
end
end)
-- Ending all walking animations
uis.InputEnded:Connect(function(input, isTyping)
local KeyCode = input.KeyCode
if Keys[KeyCode] and not isTyping then
Keys[KeyCode]:Stop()
end
end)
Because I’m trying to make my walking animation as realistic as possible, because it’ll I’m making a game based around movement. When you’re walking IRL you don’t move the same way when walking left or right.
I fixed the issue of not stopping the animation, but now the animation stops when I release one key, and not all. For exemple if I’m pressing A and W at the same time but let go of A my animation stops playing and goes into Idle animation, It only does this in shiftlock off.
Here the code:
-- Get services
local uis = game:GetService("UserInputService")
local Players = game:GetService("Players")
-- Variables
local Player = Players.LocalPlayer
Player.CharacterAdded:Wait()
local Character = Player.Character
local hum = Character:WaitForChild("Humanoid")
Player.Character:WaitForChild("Animate").Enabled = false
-- Animations
local forwardAnim = script:WaitForChild("ForwardAnimation")
local backwardAnim = script:WaitForChild("BackwardsAnimation")
local leftAnim = script:WaitForChild("LeftAnimation")
local rightAnim = script:WaitForChild("RightAnimation")
-- Tracks
local forwardTrack = hum:LoadAnimation(forwardAnim)
local backwardTrack = hum:LoadAnimation(backwardAnim)
local leftTrack = hum:LoadAnimation(leftAnim)
local rightTrack = hum:LoadAnimation(rightAnim)
-- Different keys to different tracks
local Keys = {
[Enum.KeyCode.W] = forwardTrack,
[Enum.KeyCode.S] = backwardTrack,
[Enum.KeyCode.A] = leftTrack,
[Enum.KeyCode.D] = rightTrack
}
-- Functions
-- Stops all currently running animations
function StopAll()
for _,v in pairs(Keys) do
v:Stop()
end
end
-- Detect player input and
uis.InputBegan:Connect(function(input, isTyping)
local KeyCode = input.KeyCode
-- Shiftlock on part
if uis.MouseBehavior == Enum.MouseBehavior.LockCenter then
if Keys[KeyCode] and not isTyping then
StopAll();Keys[KeyCode]:Play()
end
-- Shiftlock off part
else
if Keys[KeyCode] and not isTyping then
StopAll();forwardTrack:Play()
end
end
end)
-- Ending all walking animations
uis.InputEnded:Connect(function(input, isTyping)
local KeyCode = input.KeyCode
-- Shiftlock on
if uis.MouseBehavior == Enum.MouseBehavior.LockCenter then
if Keys[KeyCode] and not isTyping then
Keys[KeyCode]:Stop()
end
-- Shiftlock off
else
if Keys[KeyCode] and not isTyping then
StopAll();forwardTrack:Stop()
end
end
end)
It’s the whole script, as I said, I’m trying to make this as realistic as possible, which means that I do need to play a different animation when pressing W, A, S or D