I am trying to make it o that when my tool is equipped and I press my Left Mouse Button is clicked an animation plays. But there are no errors but the animation does not play. Can anyone help.
Thank You!!
Local Script
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Dumbbell = game.StarterPack.Dumbbell
local player = Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:Wait()
end
local function onInputBegan(input, gameProcessed)
if Dumbbell.Equipped and input.UserInputType == Enum.UserInputType.MouseButton1 then
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local liftAnimation = Instance.new("Animation")
liftAnimation.AnimationId = "rbxassetid://6988682446"
local liftAnimationTrack = animator:LoadAnimation(liftAnimation)
liftAnimationTrack:Play()
wait(3)
liftAnimationTrack:Stop()
end
end
UserInputService.InputBegan:Connect(onInputBegan)
But I assume you don’t have a boolean for it yet, you should make a boolean in the script to let the script know if the tool is equipped or not using .Equipped and .Unequipped events.
Sample script:
equip = false --Boolean we will use for the check.
game.Players.LocalPlayer.Backpack.Dumbell.Equipped:Connect(function()
equip = true
end
game.Players.LocalPlayer.Backpack.Dumbell.Unequipped:Connect(function()
equip = false
end
And I noticed that the Dumbbell variable you assigned is assigned to StarterPack instead of the player’s actual backpack, you can fix this by pathing to LocalPlayer.Backpack.Dumbbell instead of the StarterPack, the StarterPack is what will be cloned to player’s backpacks. Not the local player’s backpack.
Oh, basically you will have to adapt it in your current script. Mine was just an example on how to do the events and booleans.
Let me edit your script.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
local Dumbbell = player.Backpack.Dumbbell
equip = false
if not character or not character.Parent then
character = player.CharacterAdded:Wait()
end
Dumbell.Equipped:Connect(function()
equip = true
end
Dumbell.Unequipped:Connect(function()
equip = false
end
local function onInputBegan(input, gameProcessed)
if equip == true and input.UserInputType == Enum.UserInputType.MouseButton1 then
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local liftAnimation = Instance.new("Animation")
liftAnimation.AnimationId = "rbxassetid://6988682446"
local liftAnimationTrack = animator:LoadAnimation(liftAnimation)
liftAnimationTrack:Play()
wait(3)
liftAnimationTrack:Stop()
end
end
UserInputService.InputBegan:Connect(onInputBegan)
There you go, I transitioned my script into yours. Let me know how it goes.