So I want to make a tool that has different idle, jumping, walking and landing animations from the default. These animations also use every part of the Roblox model. When I unequip the tool all the animations return to normal except when I am jumping. When I unequip while jumping the falling animation will play continuously.
Here is my code. If you find any other problems please point them out. I am still relatively new to Roblox scripting in general.
Note: Some of the code in this script was is an attacking and throwing event. I only copied parts that had to do with the animations. hopefully I didn't miss anything. ????
--Animations--
local AnimIds = {
tool.KnifeIdle,
tool.KnifeRun,
tool.KnifeJump,
tool.KnifeFreeFall
}
Character:WaitForChild("Humanoid")
local Animations = {
KnifeIdleAnimation = Character.Humanoid:LoadAnimation(AnimIds[1]),
KnifeRunAnimation = Character.Humanoid:LoadAnimation(AnimIds[2]),
KnifeJumpAnimation = Character.Humanoid:LoadAnimation(AnimIds[3]),
KnifeFreeFallAnimation = Character.Humanoid:LoadAnimation(AnimIds[4])
}
--AnimationEvents--
local function Initialrunning(speed)
if IsEquiped.Value == false then return end
if speed > 1 then
NewAnim = Animations.KnifeRunAnimation
NewAnim:Play()
else
NewAnim = Animations.KnifeIdleAnimation
NewAnim:Play()
end
end
local function Running(speed)
if IsEquiped.Value == false then return end
if speed > 0.75 then
if Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Jumping or Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
if NewAnim == Animations.KnifeRunAnimation then return end
OldAnim = NewAnim
NewAnim = Animations.KnifeRunAnimation
OldAnim:Stop()
NewAnim:Play()
end
elseif speed < 0.75 then
if Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Jumping or Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
if NewAnim == Animations.KnifeIdleAnimation then return end
OldAnim = NewAnim
NewAnim = Animations.KnifeIdleAnimation
OldAnim:Stop()
NewAnim:Play()
end
end
end
local function Jumping(isActive)
if IsEquiped.Value == false then return end
OldAnim = NewAnim
NewAnim = Animations.KnifeJumpAnimation
OldAnim:Stop()
NewAnim:Play()
wait(.3)
OldAnim = NewAnim
NewAnim = Animations.KnifeFreeFallAnimation
OldAnim:Stop()
NewAnim:Play()
end
local function Standing(oldState, NewState)
if IsEquiped.Value == false then return end
if NewState == Enum.HumanoidStateType.None then
print("Standing")
end
end
local function Freefall(isActive)
if IsEquiped.Value == false then return end
if (JumpAnimTime.Value <= 0) then
if NewAnim == Animations.KnifeFreeFallAnimation then return end
OldAnim = NewAnim
NewAnim = Animations.KnifeFreeFallAnimation
OldAnim:Stop()
NewAnim:Play()
end
end
--OnEquip&UnequipEvents--
tool.Equipped:Connect(function()
IsEquiped.Value = true
Character:WaitForChild("Humanoid")
local HumRootPart = Character:WaitForChild("HumanoidRootPart")
local Speed = HumRootPart.Velocity -- returns a vector3 so
print(Speed)
local SpeedX = math.abs(Speed.X)
local SpeedY = math.abs(Speed.Y)
local SpeedZ = math.abs(Speed.Z)
if SpeedX > 2 or SpeedZ > 2 and SpeedY < 0.2 then
NewAnim = Animations.KnifeRunAnimation
NewAnim:Play()
elseif SpeedY > 0.5 then
NewAnim = Animations.KnifeFreeFallAnimation
NewAnim:Play()
else
NewAnim = Animations.KnifeIdleAnimation
NewAnim:Play()
end
CAT:BindAction("ThrowEvent", Throw, false, Enum.UserInputType.MouseButton1)
CAT:BindAction("AttackMode", InteractionKeys, false, Enum.KeyCode.Q)
end)
tool.Unequipped:Connect(function()
IsEquiped.Value = false
for index, value in pairs(Animations) do
value:Stop()
end
CAT:UnbindAction("ThrowEvent")
CAT:UnbindAction("AttackMode")
end)
--
Character.Humanoid.Running:Connect(Running)
Character.Humanoid.Jumping:Connect(Jumping)
Character.Humanoid.FreeFalling:Connect(Freefall)
Character.Humanoid.StateChanged:Connect(Standing)
UIS.InputBegan:Connect(InputBeginEvents)
UIS.InputEnded:Connect(InputEndedEvents)
