[BROKEN SCRIPT] Custom Seat Animation w/ a Particle Emitter

You can write your topic however you want, but you need to answer these questions:

  1. I want the script to play an animation when a player sits on the seat. Also when it finds a player that sat on the seat, I also want it to spawn in a particle emitter from “SeverStorage” onto the players head.

  2. The problem here is the animation doesn’t stop playing and the particle emitter never even spawned onto the player’s head.

  3. i’ve tried sending a screenshot of the script onto HiddenDevs code - help. Nothing worked.

I’m a beginner scripter. Just started 2 weeks ago and I’ve got to say this is most difficult script I’ve ever done. Been stuck on it for a few days now.

– This is an example Lua code block

Hope this help you :

-- / Service(s)
local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")

-- Stuff(s)
local Particle = ServerStorage:FindFirstChild("ParticleEmitter")
local Seat = script.Parent

-- AnimationsTrack(s)
local AnimTrack

-- Animation(s)
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://13113598813"

local PrimaryPart

-- Check if the player is sitting on the seat
RunService.Heartbeat:Connect(function()
	if Seat.Occupant ~= nil then
		local Character = Seat.Occupant.Parent -- Get the Humanoid's/Occupant's parent or in short, just make it get the character
		
		local Humanoid = Character:FindFirstChild("Humanoid")
		PrimaryPart = Character.PrimaryPart -- Correct me if I'm wrong, but they has changed the PrimaryPart to Head now
		
		-- If Head does not have "ParticleEmitter" then, make it have one and parent it on the head
		if PrimaryPart:FindFirstChild("ParticleEmitter") == nil then
			local ClonedParticle = Particle:Clone()
			ClonedParticle.Parent = PrimaryPart
			
			-- Load the animation and play it
			AnimTrack = Humanoid:LoadAnimation(Animation)
			AnimTrack:Play()
		end
	else
		-- If the "AnimTrack" isn't nil then, stop the "AnimTrack" and also delete the "ParticleEmitter"
		if AnimTrack ~= nil then
			local specifiedParticle = PrimaryPart:FindFirstChild("ParticleEmitter")
			specifiedParticle:Destroy()
			AnimTrack:Stop()
		end
	end
end)
  1. Occupant is a HumanoidObject
  2. The child of the HumanoidObject, Animator should be used to load animations.
  3. You do not store the particleClone, so when you try and :Destroy() it you just :Destroy() your newly created one.
  4. Also I prefer :GetPropertyChangedSignal(...) instead of .Changed when detecting 1 property changed.

Now there are few mistakes we can gather from this also.

  1. You try and get a child called “Head” which is parented to the Occupant
  2. You load the animation on the Occupant not on Occupant.Animator
  3. You :Clone() unneededly when someone stops sitting on the seat.

Instead have something like:

local playingAnim, particleClone

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
   -- Stop
   if playingAnim then
      playingAnim:Stop() playingAnim = nil
   end

   if particleClone then
      particleClone:Destroy()
   end
   -- Start
   local occupant = seat.Occupant
   if not occupant or not occupant.Parent then return end

   local animator, head = occupant:FindFirstChildWhichIsA("Animator"), occupant.Parent:FindFirstChild("Head")

   if animator then
      playingAnim = occupant:LoadAnimation(newAnim)
      playingAnim:Play()
   end

   if head then
      particleClone = particleEmit:Clone()
      particleClone.Parent = head
   end
end)

Wow, so many mistakes.
I’m not sure but do I replace your code with the function I made? Because I did it and the animation part works but not the particle emitter. It didn’t spawned.

Make sure head is not nil.
Another issue could be that the Particle Emitter has not been Enabled. So check if the ParticleEmitter is parented to your head.

print(head)

particleClone.Enabled = true

Finally! It works and man o man this is a good feeling. Thanks btw and I’ll make sure to learn more.

A tip from an subjectively advanced programmer to beginner is to spam prints everywhere.
Also think of your end goal and how you want to reach it.

2 Likes

Spam prints everywhere as in like debugging? Noted.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.