I made an animation that I want to play when the player clicks a part.
I have a script but I’m unsure how to implement ClickDetector.
the local script has the animation in it, its in StarterPlayerScripts. I read docs on Animator and Animation, which is how I got the script, but yea I just don’t know how to add scripts for ClickDetector without it showing an error.
script:
local function playAnimationFromServer(character, animation)
local clickDetector = workspace.part.ClickDetector
local humanoid = character:FindFirstChildOfClass("Humanoid")
clickDetector.Clicked:Connect(function()
if humanoid then
local animator = humanoid:FindFirstChildOfClass("Animator")
if animator then
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
return animationTrack
end
end
end)
animation.AnimationId = "rbxassetid://(id)"
local character = script.Parent
playAnimationFromServer(character, animation) -- error at 'animation' says 'did you forget to close 'then' at line 7?' but when I add another end, it makes the other ends have errors
I wrote some quick code and tested it. (It should work)
Here you can see how the click event should be used outside of a function (raw)
----- this code will check if the player clicked the part. then play the animation from id
local player = game.Players.LocalPlayer
local clickdetector = workspace.Part.ClickDetector
local id = 0000
clickdetector.MouseClick:Connect(function(playerThatClicked)
if playerThatClicked ~= player then return end ---- only fire if the player that clicked is myself
local character = playerThatClicked.Character --- get the character
local humanoid = character:WaitForChild("Humanoid") --- get humanoid
local animation = Instance.new("Animation") --- make new animation
animation.AnimationId = `rbxassetid://{id}` --- set animation
local animator = humanoid:FindFirstChildOfClass("Animator")
if animator then
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
end
end)
Here you can see how to make a function that makes a new event for you with given information through parameters.
As you can see instead of defining the variables on top of the page. I now get them from the parameters. Allowing me to use this code as many time as i want. With different ClickDetectors and Animations.
----- this function allows you to set any clickdetector to play any animation
local player = game.Players.LocalPlayer
function setClickDetector (clickdetector, animationID)
clickdetector.MouseClick:Connect(function(playerThatClicked)
if playerThatClicked ~= player then return end ---- only fire if the player that clicked is myself
local character = playerThatClicked.Character --- get the character
local humanoid = character:WaitForChild("Humanoid") --- get humanoid
local animation = Instance.new("Animation") --- make new animation
animation.AnimationId = `rbxassetid://{animationID}` --- set animation
local animator = humanoid:FindFirstChildOfClass("Animator")
if animator then
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
return animationTrack
end
end)
end
setClickDetector(workspace.Part.ClickDetector, 0000) --- call the function to set a clickdetector event