So here I have a script, the task is to make that when you click played animation of how the player throws the bomb. And then the bomb explodes when it touches some part. Now I am doing so that the animation just played, but for some reason it does not work, why?
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local bombModel = workspace["TNT STICK"]
local explosionAnimation = game:GetService("ReplicatedStorage").Throwing -- Replace 'ExplosionAnimation' with the actual name of your explosion animation
local function throwBomb()
--local clone = bombModel:Clone()
--clone.Parent = workspace
--clone.Parent = player.Character.RightHand
local Humanoid = player.Character:WaitForChild("Humanoid")
local Animator = Humanoid:FindFirstChildOfClass("Animator")
--local animation = clone:FindFirstChild("Animation") -- Replace 'Animation' with the actual name of the animation within the bomb model
local animation = Animator:LoadAnimation(explosionAnimation)
animation:Play()
--clone:SetPrimaryPartCFrame(player.Character.RightHand.CFrame)
print("Throw")
end
mouse.Button1Down:Connect(throwBomb)
In the provided script, the issue seems to be with how youβre using the Animator and LoadAnimation methods. Hereβs an updated version of the script that should work correctly:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local bombModel = workspace["TNT STICK"]
local explosionAnimation = game:GetService("ReplicatedStorage").Throwing -- Replace 'ExplosionAnimation' with the actual name of your explosion animation
local function throwBomb()
local Humanoid = player.Character:WaitForChild("Humanoid")
local Animator = Humanoid:FindFirstChildOfClass("Animator")
if Animator then
local animation = Animator:LoadAnimation(explosionAnimation)
animation:Play()
end
end
mouse.Button1Down:Connect(throwBomb)
In this updated version, the Animator is obtained from the playerβs Humanoid instead of searching for it within the bomb model. It then checks if the Animator exists before loading and playing the animation. This should ensure that the animation plays properly when the player clicks.
Make sure to replace 'Throwing' with the actual name of your explosion animation within the ReplicatedStorage. Also, make sure that the animation is properly set up and uploaded to the game.