Hey! I’m trying to detect if the player is moving while having a sword, and if they are, they should play a walking animation. If they are not, it should stop the walking animation (if its playing) and play the idle animation.
However, I cannot detect if the player has a sword or not… I tried creating a separate variable that changes if the player has equipped or unequipped the sword to no avail… I even tried checking if the player had the sword tool in their character and the script could not find that. So what should I do? Here is my code, this is in a local script and has zero errors.
local Sword = script.Parent
local Anims = Sword:WaitForChild("Animations")
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local c = Player.Character
local Anim = c:WaitForChild("Humanoid").Animator:LoadAnimation(Anims.FinalGripPose)
local HasSword = false
local Walk = c.Humanoid.Animator:LoadAnimation(Anims.FinalRunning)
Sword.Equipped:Connect(function()
Anim:Play()
HasSword = true
end)
local function check_vel()
while true do
wait(0.6)
if c.HumanoidRootPart.Velocity ~= Vector3.new(0,0,0) and c:FindFirstChild("Sword") then
for i, v in pairs(c.Humanoid.Animator:GetPlayingAnimationTracks()) do
print(v.Name)
if v.Name == "WalkAnim" or v.Name == "RunAnim" or v.Name == "FinalGripPose" or v.Name == "ToolNoneAnim" then
v:Stop()
end
end
Walk:Play()
elseif c.HumanoidRootPart.Velocity == Vector3.new(0,0,0) and c:FindFirstChild("Sword") then
Walk:Stop()
Anim:Play()
else
return
end
end
end
spawn(check_vel)
Sword.Unequipped:Connect(function()
Anim:Stop()
HasSword = false
end)
You do not need to check the Velocity instead there is an event that can be used by the humanoid.
The StateChanged event will fire whenever the humanoid state type changes.
For help here is the API reference: Humanoid.StateChanged (roblox.com)
That isn’t the problem, its that I can’t tell if the player has or doesn’t have a sword. I checked if they have it within their character and even created a variable that changes if the sword is equipped or unequipped. Both didn’t work…
The reason is probably because of the while loop. The while loop keeps running not allowing the script to execute anything else outside the block.
Try wrapping the while loop in a coroutine.
But I used the spawn() function to create a new thread for my function so that it doesn’t interrupt the rest of my script…
Also, how would you use a coroutine? I’ve never used one before.
Ah I am sorry I failed to notice it. In this case spawn and coroutine both will do the same work so I think that might not be the problem.
I hope this attracts some attention from someone else who can help you.
I though will reply if I can solve this
But make sure you don’t use the velocity method as it can be inaccurate and studio has already built a function to avoid these and you won’t need to use a coroutine or spawn which will make the code clearer.
No no, thanks for the help! Guess ill just wait for help, although I’ll keep trying out new things and checking the API for things that could help me detect if the player has a sword.
When I make this, I make, when equip a item, the animation script switch to other in ReplicatedStorage, if unequip, switch to the old animation script.
The main animation script can be found inside the player character.
The code looks reasonable. Why do you not use the HasSword variable? The FindFirstChild("Sword") probably doesn’t work because the sword is not directly in the character, most likely it’s under some hand part.