Script running animation when mouse clicked is not working

In ServerStorage, I have a tool (it’s a sunscreen bottle) and inside I have a local script.
Screen Shot 2021-08-23 at 10.01.16 AM

Script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")

local tool = script.Parent 

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7238904032"

local loaded = Animator:LoadAnimation(animation)

tool.Activated:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		loaded:Play()
	end)
end)

I’m not 100% sure, but have you tried removing the mouse.Button1Down event?

I have but it still doesn’t do anything even if its just equipped

Are you sure the properties are set correctly?

like for the tool or what because im pretty sure I set everything correctly

How are you getting the tool, since I don’t see any parenting for the tool anywhere?
(Sorry for the late reply)

Do you have a click detector in the object ? I don’t see it.

Please, next time inform us about errors in the Output.

Tool.Activated does not send any arguments, and it is fired when the player clicks with the mouse (without Ctrl being pressed), so there is no need to listen when the mouse presses inside the function.

Your code should look like this:

local player = game.Players.LocalPlayer
local character = script.Parent.Parent
local Animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")

local tool = script.Parent 

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7238904032"

local loaded = Animator:LoadAnimation(animation)

tool.Activated:Connect(function()
	loaded:Play()
end)

This didn’t work for me but ill show you the output


Also I forgot to mention so this is in server storage at first but then if the player purchases the sunscreen with a GUI it puts it in their backpack which is why it says its in the backpack now

it says local tool = script.Parent

You don’t need click detectors for tools, there is a built in API that you can use which is tool.Activated

1 Like

try this

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")

local tool = script.Parent 

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7238904032"

local loaded = Animator:LoadAnimation(animation)

tool.Activated:Connect(function()
	loaded:Play()
end)
1 Like

It still doesn’t work do you think it has to do with the part that it gets parented to the players backpack when you buy it?

It might, but I also think it might be something else. Did it have any errors with that code?

i see no errors in the output other than some voice chat thing

Can you add a print(“hello”) when the tool is activated? Just to make sure the script is working

1 Like


it does the hello thing

Hmm, are you the owner of the animation that’s playing? If you are, is the game you’re working on a group game?

yea I made the animation im not sure how well I did it but it should be working bc if I test it on a dummy it plays the animation although I am new to animations as this is my first time working with them

Is the game a group game? If it is that might be the issue because it might not have the owner of the animation set up correctly.