Particles don't disable when player stops walking

Hey everyone! So recently I made a script that enables particles in your feet when you walk on specific materials, It works well so far but I had one small Issue. The particles don’t get disabled when I stop moving and I wanted to know how I can fix that.

Here’s the script

local humanoid = script.Parent:FindFirstChild("Humanoid")
local HumanoidRootPart = script.Parent.HumanoidRootPart

humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()



if humanoid.FloorMaterial == Enum.Material.Snow then

	script.Parent.LeftFoot.ParticlePart.Enabled = true

	script.Parent.RightFoot.ParticlePart.Enabled = true


elseif humanoid.FloorMaterial == Enum.Material.Grass then

	script.Parent.LeftFoot.ParticlePart.Enabled = false

	script.Parent.RightFoot.ParticlePart.Enabled = false

end
end)

I’m new to scripting and I would love if you guide me to what exactly I should do or replace since I’ve had this issue for a really long time and there isn’t any video explaining it so I figured I should put it here so it’s more specific!

I don’t really see anything here checking that the player is currently walking/moving so, in order to check you use a loop saying

if humanoid.MoveDirection.Magnitude > 0 then
particle.Enabled = true
end

Little example:

Perhaps constantly setting the properties to what they already are could be using more resources, there’s room for improvement, but it works.

(Just saying in those loops I meant to right (leftFoot:GetChildren() and rightFoot:GetChildren()), error on my part.)

Does this work?
image

You need to use a loop to check if the player is moving, this is only checking that every time the material underneath the player changes.

I used RunService.Stepped but you can try using a while loop which will update slower but that’s fine.

Where exactly is RunService, it says unknown global

game:GetService("RunService").Stepped:Connect(function()
-- ur code here runs every frame
end)

Take into account that when acquiring services, ServerScriptService, TweenService, RunService you should use game:GetService()


idk why it says this

You misspelled MoveDirection, you forgot the t.

For some reason it still doesn’t works

local humanoid = script.Parent:FindFirstChild("Humanoid")
local leftFoot = script.Parent.LeftFoot
local rightFoot = script.Parent.RightFoot

game:GetService("RunService").Stepped:Connect(function()
	if humanoid.MoveDirection.Magnitude > 0 then



		if humanoid.FloorMaterial == Enum.Material.Snow then
			script.Parent.RightFoot.ParticlePart.Enabled = true;
			script.Parent.LeftFoot.ParticlePart.Enabled = true;
		end


	else

		for _,  emitter in pairs (leftFoot) do
			if emitter:IsA("ParticleEmitter") then
				emitter.Enabled = false;
			end
		end
		for _, emitter in pairs (rightFoot) do
			if emitter:IsA("ParticleEmitter") then 
				emitter.Enabled = false;
			end
		end
	end
end)

In the for loops leftFoot and rightFoot should have :GetChildren() on the end, that was my mistake in the image I sent. Just to make things easier for you, here’s a working example (not material specific though that should be easy to adjust), you can build it from here. Copy this in a script and put it in the ServerScriptService.

local players = game:GetService("Players");
local run = game:GetService("RunService");

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
		local humanoid = character:WaitForChild("Humanoid");
		local leftFoot, rightFoot = character:WaitForChild("LeftFoot"), character:WaitForChild("RightFoot")
		local leftEmitter, rightEmitter = Instance.new("ParticleEmitter", leftFoot), Instance.new("ParticleEmitter", rightFoot)
		leftEmitter.Enabled, rightEmitter.Enabled = false, false;
		
		run.Stepped:Connect(function(delta)
			if humanoid.MoveDirection.Magnitude > 0 then
				
				if not leftEmitter.Enabled and not rightEmitter.Enabled then
					leftEmitter.Enabled, rightEmitter.Enabled = true, true
				end
				
			else
				
				if leftEmitter.Enabled and rightEmitter.Enabled then
					leftEmitter.Enabled, rightEmitter.Enabled = false, false
				end
				
			end
		end)
		
	end)
end)

It works but now it doesn’t stop when the material changes

local players = game:GetService("Players");
local run = game:GetService("RunService");

		
		local humanoid = script.Parent:WaitForChild("Humanoid");
        local leftFoot, rightFoot = script.Parent:WaitForChild("LeftFoot"), script.Parent:WaitForChild("RightFoot")
        local leftEmitter, rightEmitter = leftFoot.ParticlePart, rightFoot.ParticlePart
		leftEmitter.Enabled, rightEmitter.Enabled = false, false;
		
		run.Stepped:Connect(function(delta)
			if humanoid.MoveDirection.Magnitude > 0 then
				
				if not leftEmitter.Enabled and not rightEmitter.Enabled then
					leftEmitter.Enabled, rightEmitter.Enabled = true, true
				end
				
			else
				
				if leftEmitter.Enabled and rightEmitter.Enabled then
					leftEmitter.Enabled, rightEmitter.Enabled = false, false
				end
				
			end
end)

humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()



	if humanoid.FloorMaterial == Enum.Material.Snow then

		leftEmitter.Enabled = true

		rightEmitter.Enabled = true


	elseif humanoid.FloorMaterial == Enum.Material.Grass then

		leftEmitter.Enabled = false

		rightEmitter.Enabled = false
	end
end)

Yes I had just realised that and I’m figuring out a solution now, so hold on.

Instead of organising your if material statements with elseif's, use individual loops that just say else like this:

Mine is working for grass and snow currently,

local players = game:GetService("Players");
local run = game:GetService("RunService");

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)

		local humanoid = character:WaitForChild("Humanoid");
		local leftFoot, rightFoot = character:WaitForChild("LeftFoot"), character:WaitForChild("RightFoot")
		
		local leftGrassParticle, rightGrassParticle = Instance.new("ParticleEmitter", leftFoot), Instance.new("ParticleEmitter", rightFoot)
		leftGrassParticle.Color, rightGrassParticle.Color = ColorSequence.new(Color3.fromRGB(0, 255, 0), Color3.fromRGB(0, 255, 0)), ColorSequence.new(Color3.fromRGB(0, 255, 0), Color3.fromRGB(0, 255, 0))
		local leftSnowParticle, rightSnowParticle = Instance.new("ParticleEmitter", leftFoot), Instance.new("ParticleEmitter", rightFoot)
		
		local function disableAllParticles ()
			for _, particle in pairs(leftFoot:GetChildren()) do
				if particle:IsA("ParticleEmitter") then
					particle.Enabled = false;
				end
			end
			for _, particle in pairs(rightFoot:GetChildren()) do
				if particle:IsA("ParticleEmitter") then
					particle.Enabled = false;
				end
			end
		end
		
		run.Stepped:Connect(function(delta)
			if humanoid.MoveDirection.Magnitude > 0 then
				
				-- grass
				if humanoid.FloorMaterial == Enum.Material.Grass then
					leftGrassParticle.Enabled, rightGrassParticle.Enabled = true, true
				else
					leftGrassParticle.Enabled, rightGrassParticle.Enabled = false, false
				end
				
				-- snow
				if humanoid.FloorMaterial == Enum.Material.Snow then
					leftSnowParticle.Enabled, rightSnowParticle.Enabled = true, true
				else
					leftSnowParticle.Enabled, rightSnowParticle.Enabled = false, false
				end
				
			else
				disableAllParticles();
			end
		end)

	end)
end)
1 Like

It works! tysm you helped me out a lot!!

1 Like

Sorry for making you send so many messages, I didn’t mean for this to be as tedious as it was cause I thought it was simpler lol. Glad I could help :grin:

1 Like