So I’m making a animation script that works by changing the core animations ID,however I am making a shooter and I want the script to change the ID everytime the player equips a gun in specific.
The reason why I used a while loop is to check every time if the player has a gun by using if statements.
This is’nt a local script and it’s parent is ServerScriptService
local Players = game:GetService("Players")
------------------------------------------------ animations variables
local runanimation = "rbxassetid://16430279776"
local idleAnimation = "rbxassetid://16656585235"
------------------------------------------------
local submachinegunsidleAnimation = "rbxassetid://16794852006"
local submachinegunswalkingAnimation = "rbxassetid://16794893307"
------------------------------------------------
local pistolidleAnimation = "rbxassetid://16651714234"
local pistolwalkingAnimation = "rbxassetid://16651622905"
------------------------------------------------
local function OnCharacterAdded(character)
Players.PlayerAdded:Connect(function(Player)
if true then print("Yay") end
while true do
wait(0.5)
local hum = character:FindFirstChild("Humanoid")
local AnimateScript = character:WaitForChild("Animate")
------------------------------------------------------
if Player:FindFirstChildOfClass("Tool"):FindFirstChild("Pistol") then --Changing the animations when equipping a pistol
AnimateScript.run.RunAnim.AnimationId = pistolwalkingAnimation
AnimateScript.idle.Animation1.AnimationId = pistolidleAnimation
end
if Player:FindFirstChildOfClass("Tool"):FindFirstChild("Submachineguns") then --Changing the animations when equipping a Submachinegun
AnimateScript.run.RunAnim.AnimationId = submachinegunswalkingAnimation
AnimateScript.idle.Animation1.AnimationId = submachinegunsidleAnimation
end
AnimateScript.run.RunAnim.AnimationId = runanimation
AnimateScript.idle.Animation1.AnimationId = idleAnimation
end
end)
end
local function onPlayerAdded(player)
player.CharacterAppearanceLoaded:Connect(OnCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Ok I think I fixed a part of the script,now it plays the default animations but it does’nt play the gun-walking animations
local Players = game:GetService("Players")
------------------------------------------------ animations variables
local runanimation = "rbxassetid://16430279776"
local idleAnimation = "rbxassetid://16656585235"
------------------------------------------------
local submachinegunsidleAnimation = "rbxassetid://16794852006"
local submachinegunswalkingAnimation = "rbxassetid://16794893307"
------------------------------------------------
local pistolidleAnimation = "rbxassetid://16651714234"
local pistolwalkingAnimation = "rbxassetid://16651622905"
------------------------------------------------
local function OnCharacterAdded(character)
while true do
wait(0.5)
local hum = character:FindFirstChild("Humanoid")
local AnimateScript = character:WaitForChild("Animate")
------------------------------------------------------
if character:FindFirstChildOfClass("Tool") then --Changing the animations when equipping a pistol
if character:FindFirstChildOfClass("Tool"):FindFirstChild("Pistol") then
AnimateScript.run.RunAnim.AnimationId = pistolwalkingAnimation
AnimateScript.idle.Animation1.AnimationId = pistolidleAnimation
end
end
if character:FindFirstChildOfClass("Tool") then --Changing the animations when equipping a Submachinegun
if character:FindFirstChildOfClass("Tool"):FindFirstChild("Submachinegun") then
AnimateScript.run.RunAnim.AnimationId = submachinegunswalkingAnimation
AnimateScript.idle.Animation1.AnimationId = submachinegunsidleAnimation
end
end
AnimateScript.run.RunAnim.AnimationId = runanimation
AnimateScript.idle.Animation1.AnimationId = idleAnimation
end
end
local function onPlayerAdded(player)
player.CharacterAppearanceLoaded:Connect(OnCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Instead of doing all of this you should put a local script inside your tool then create a reference to your animations and detect when a players equips the tool after that check every frame if the player is moving and play its corresponding animation etc.
something like this :
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local weapon :Tool = script.Parent
local runAnimation = humanoid:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("RunAnimation")) --When the player moves
local idleAnimation = humanoid:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("IdleAnimation")) -- When the player isn't moving
local RunService = game:GetService("RunService")
local connection = nil
weapon.Equipped:Connect(function() -- Check when the weapon is equipped
connection = RunService.Heartbeat:Connect(function() --This function will run every frame
if humanoid.MoveDirection.Magnitude > 0 then -- Checks If the player is moving
if idleAnimation.Playing then -- If The Idle Animation Is Playing
idleAnimation:Stop()
end
if not runAnimation.Playing then --If the animation isn't playing already
runAnimation:Play()
end
else --If He's Not Moving
if runAnimation.Playing then -- If The Running Animation Is Playing
runAnimation:Stop()
end
if not idleAnimation.Playing then --If the animation isn't playing already
idleAnimation:Play()
end
end
end)
end)
weapon.Unequipped:Connect(function() --Detect When The Player Unnequipped The Weapon
connection:Disconnect() -- Stop Playing The Animations
end)
You can even check if the player is jumping or falling or landed using humanoidState