im trying to make walking animation and stuff but movement direction.magnitude doesnt work unless its going into a corner and i cant figure out the problem and im just confused please help
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum:Humanoid = char:WaitForChild("Humanoid")
local humrootpart = plr.Character:WaitForChild("HumanoidRootPart")
local animator = hum:WaitForChild("Animator")
local anim = animator:LoadAnimation(script:WaitForChild("Animation"))
local iswalk = false
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local function iswalking ()
if iswalk then
anim:Play()
print(112)
end
end
local function isknawtwalking ()
if not iswalk then
anim:Stop()
print(11)
end
end
local movementkeys = {
Enum.KeyCode.W,
Enum.KeyCode.A,
Enum.KeyCode.S,
Enum.KeyCode.D
}
local function checkis()
if hum.WalkSpeed > 0 then
print(1)
iswalk = true
iswalking()
--end
end
end
local function checkknawt()
if hum.WalkSpeed <= 0 then
print(1)
iswalk = false
isknawtwalking()
--end
end
end
uis.InputBegan:Connect(checkis)
uis.InputEnded:Connect(checkknawt)
So here’s an example code I made which fixes the animation autoplaying. (Does what the original intent of what I took from your script that you were trying to achieve.).
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum:Humanoid = char:WaitForChild("Humanoid")
local humrootpart = plr.Character:WaitForChild("HumanoidRootPart")
local animator = hum:WaitForChild("Animator")
local anim = animator:LoadAnimation(script:WaitForChild("Animation"))
local iswalk = false
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local function iswalking ()
if iswalk then
anim:Play()
print(112)
end
end
local function isknawtwalking ()
if not iswalk then
anim:Stop()
print(11)
end
end
--[[local movementkeys = {
Enum.KeyCode.W,
Enum.KeyCode.A,
Enum.KeyCode.S,
Enum.KeyCode.D
}]] -- Blanked this out as it's not being used. Can change this back if you wish.
local function checkis(Input)
if uis.InputBegan then
if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
print(1)
iswalk = true
iswalking()
else
end
--end
end
end
local function checkknawt(Input)
if uis.InputEnded then
if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
print(2)
iswalk = false
isknawtwalking()
else
end
--end
end
end
uis.InputBegan:Connect(checkis)
uis.InputEnded:Connect(checkknawt)
The issue was that WalkSpeed value from the Humanoid does not change automatically, it’s always at 16 unless it’s changed by another script or from the Studio/User, that even counts for when the player chooses to not walk, be idle and/or just stay still without touching the keyboard at all. Movement and Velocity are different in this case so something important to note.
I’ve decided to update your script by changing the if statements to use the UserInputService more effectively here, so I wish this helps you with your project and progress on your work.
Also this post should’ve probably been posted in Help and Feedback > Scripting Support as Help and Feedback > Code Review is more focused on supplementary help as opposed to necessary where the code simply does not do what you want it to (necessary) versus improving it beyond what you already wanted it to do (supplementary).
Maybe you can use the animator localscript inside the player model? from what I gathered, you want to make a walk animation script and doing what I said will be just fine
ok so for some reason if i click two of the buttons it goes back to the default walking animation and i dont really know why, im gonna try adding the movement buttons back and see if that fixes it
game["Run Service"].Heartbeat:Connect(function()
if Humanoid.MoveDirection.Magnitude > 0 then
if Animation.IsPlaying == false then
Animation:Play()
end
else
Animation:Stop()
end
end)
It’s resetting and overlapping the same animation because there is no detection to see if the said animation is already playing. Try this.
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum:Humanoid = char:WaitForChild("Humanoid")
local humrootpart = plr.Character:WaitForChild("HumanoidRootPart")
local animator = hum:WaitForChild("Animator")
local anim = animator:LoadAnimation(script:WaitForChild("Animation"))
local iswalk = false
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local function iswalking ()
if iswalk then
anim:Play()
print(112)
end
end
local function isknawtwalking ()
if not iswalk then
anim:Stop()
print(11)
end
end
--[[local movementkeys = {
Enum.KeyCode.W,
Enum.KeyCode.A,
Enum.KeyCode.S,
Enum.KeyCode.D
}]] -- Blanked this out as it's not being used. Can change this back if you wish.
local function checkis(Input)
if uis.InputBegan then
if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
if iswalk == false then
print(1)
iswalk = true
iswalking()
elseif iswalk == true then
end
else
end
--end
end
end
local function checkknawt(Input)
if uis.InputEnded then
if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
print(2)
iswalk = false
isknawtwalking()
else
end
--end
end
end
uis.InputBegan:Connect(checkis)
uis.InputEnded:Connect(checkknawt)
Sorry for the bad spacing in code, I’m on phone rn so I haven’t actually tested this and only just typed it out. Ctrl+C and then Ctrl+V into a script within the Studio should fix the spacing and/or indentation for this written code automatically.
Annex: On PC now, here’s an updated version of the same script, I corrected the spacing, indentation and such using the method above.
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum:Humanoid = char:WaitForChild("Humanoid")
local humrootpart = plr.Character:WaitForChild("HumanoidRootPart")
local animator = hum:WaitForChild("Animator")
local anim = animator:LoadAnimation(script:WaitForChild("Animation"))
local iswalk = false
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local function iswalking ()
if iswalk then
anim:Play()
print(112)
end
end
local function isknawtwalking ()
if not iswalk then
anim:Stop()
print(11)
end
end
--[[local movementkeys = {
Enum.KeyCode.W,
Enum.KeyCode.A,
Enum.KeyCode.S,
Enum.KeyCode.D
}]] -- Blanked this out as it's not being used. Can change this back if you wish.
local function checkis(Input)
if uis.InputBegan then
if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
if iswalk == false then
print(1)
iswalk = true
iswalking()
elseif iswalk == true then
end
else
end
--end
end
end
local function checkknawt(Input)
if uis.InputEnded then
if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
print(2)
iswalk = false
isknawtwalking()
else
end
--end
end
end
uis.InputBegan:Connect(checkis)
uis.InputEnded:Connect(checkknawt)
if i press 2 movement keys it goes back to the default animation, also is there like a way to make this code but without the enum.keycode,.w and so on, because im getting annoyed about it and how long it is, i know humanoid.movementdirection.magnitude could work but it needs a way to detect and run service makes the game too laggy and using the method i already have is clunky.
Here’s a solution, though you need to use a server script as well as you’ll be using remote events when using this method.
Localscript for StarterCharacterScripts:
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum:Humanoid = char:WaitForChild("Humanoid")
local humrootpart = plr.Character:WaitForChild("HumanoidRootPart")
local animator = hum:WaitForChild("Animator")
local anim = animator:LoadAnimation(script:WaitForChild("Animation"))
local iswalkremote = game:GetService("ReplicatedStorage"):FindFirstChild("iswalk")
local iswalkremotefalse = game:GetService("ReplicatedStorage"):FindFirstChild("iswalkfalse")
local iswalkremotelocal = game:GetService("ReplicatedStorage"):FindFirstChild("iswalklocal")
local iswalkremotelocalfalse = game:GetService("ReplicatedStorage"):FindFirstChild("iswalklocalfalse")
local iswalking = false
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
if plr then
uis.InputBegan:Connect(function(Input)
wait()
if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
iswalkremote:FireServer(plr)
end
end)
uis.InputEnded:Connect(function(Input)
wait()
if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.S or Input.KeyCode == Enum.KeyCode.D then
iswalkremotefalse:FireServer(plr)
end
end)
end
iswalkremotelocal.OnClientEvent:Connect(function()
if iswalking == false then
anim:Play()
iswalking = true
elseif iswalking == true then
else
end
end)
iswalkremotelocalfalse.OnClientEvent:Connect(function()
anim:Stop()
iswalking = false
end)
Server Script for Workspace or elsewhere (wherever you feel as long as it’s viable.):
local iswalkremote = game:GetService("ReplicatedStorage"):FindFirstChild("iswalk")
local iswalkremotefalse = game:GetService("ReplicatedStorage"):FindFirstChild("iswalkfalse")
local iswalkremotelocal = game:GetService("ReplicatedStorage"):FindFirstChild("iswalklocal")
local iswalkremotelocalfalse = game:GetService("ReplicatedStorage"):FindFirstChild("iswalklocalfalse")
--local player = game.Workspace.AudioCreation1
iswalkremote.OnServerEvent:Connect(function(plr)
print(plr.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude)
if plr.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude > 0 then
iswalkremotelocal:FireClient(plr)
elseif plr.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude == 0 then
iswalkremotelocalfalse:FireClient(plr)
end
end)
iswalkremotefalse.OnServerEvent:Connect(function(plr)
print(plr.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude)
if plr.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude > 0 then
iswalkremotelocal:FireClient(plr)
elseif plr.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude == 0 then
iswalkremotelocalfalse:FireClient(plr)
end
end)
I use MoveDirection.Magnitude as you mentioned that is located within the Humanoid of the player. The issue is that you were only using Localscript which is why there was no recognition of changed Vector3 magnitude values for MoveDirection property within the player character’s Humanoid parent.
I used 4 remotes, as you’ll find in both server script and local script.
I can confirm that this works thanks to experimentation from my end.
I wish you good luck with your project and pray this is what you needed/solves this issue.
OMG THANK YOU SO MUCH!!! ill try looking and analyzing this code so i can replicate/reuse this in other games or code! thx!!! surprised that movement direction magnitude wasnt allowed in local script