Disable inserted trails inside the player under 50 speed

This script should disable trails inside attachments inside of the player body parts when the player goes slower than 50 studs per second, though it doesnt…

If i remove the while <1000 and replace it with the <=50 the trail starts at speed 50 but never ends after

local player = game.Players.LocalPlayer
local character = player.Character or script.Parent
local humanoid = character.Humanoid
local remoteevent = game.ReplicatedStorage.RemoteEvent


while humanoid.WalkSpeed <1000 do
if humanoid.WalkSpeed <= 50 then
	for i,v in pairs(character:GetChildren()) do
		if v:IsA("Trail") then
				v.Enabled = false
				end
		end
	end

	wait()
end


remoteevent:FireServer()

what do exactly mean by that, it doesnt stop when the Walkspeed goes under 50?

yes, after its activated going over 50 speed, going under doesnt deactivate it

Can you try adding prints to that statement. i will help make debugging better

1 Like

Your code is quite confusing. Why do you need to check if the WalkSpeed is <1000? What is the RemoteEvent for? Here’s how I would do the code:

  • Function to listen for WalkSpeed property changing
  • Is the new WalkSpeed over or under 50? Do what has to be done if over or under 50

If you want the trail to be visible to all players, then you should use a RemoteEvent WHEN the PropertyChanged event happened as your code is in a LocalScript.

the <1000 walkspeed is so it keeps looping forever since the walkspeed will always be below 1k

you could’ve used while task.wait() do or while true do or renderstepped

You should instead try and listen for the Walkspeed. Something like this.

local Player = game.Players.LocalPlayer

local Conn

function Disable(walk)
if walk <= 1000 and walk >= 50 the 
for i,v in Player.Character: GetDescendants() do
if v:IsA("Trail") then
v.Enabled = true
end
end
else
for i,v in Player:GetDescendants() 
if v:IsA("Trail") then
v.Enabled = false
end
end
end
Conn:Disconnect()
end

Conn = Player.Character:WaitForChild("Humanoid"):GetPropertyChangedSignal("Walkspeed"):Connect(Disable())

hey, i tried this but theres no trails added at all anymore.

also, there were red lines i think you meant then with “the” and forgot a do

local Player = game.Players.LocalPlayer

local Conn

function Disable(walk)
	if walk <= 1000 and walk >= 50 then
		for i,v in Player.Character: GetDescendants() do
			if v:IsA("Trail") then
				v.Enabled = true
			end
		end
		else
	for i,v in Player:GetDescendants() do
		if v:IsA("Trail") then
			v.Enabled = false
		end
end
end
Conn:Disconnect()
end

Conn = Player.Character:WaitForChild("Humanoid"):GetPropertyChangedSignal("Walkspeed"):Connect(Disable())

If I understand what you mean by this, you’re trying to disable trail’s in the player’s character if they’re travelling under 50 WalkSpeed, and if they’re travelling above 50 WalkSpeed then enable the trails?

If so, then you can use an event to listen to the character’s walkspeed, and connect to it using GetPropertyChangedSignal which listens for a specific property change and fires if it does.
From here you can check the player’s walkspeed and then adjust the trails accordingly!
Just check your loop though, as you mention them being inside of an attachment yet you’re only looking for trails directly in the character.

youre right about the attachments, though it always worked with this script:

local player = game.Players.LocalPlayer
local character = player.Character or script.Parent
local humanoid = character.Humanoid
local remoteevent = game.ReplicatedStorage.RemoteEvent


while humanoid.WalkSpeed == 16 do
	print(humanoid.WalkSpeed)

	for i,v in pairs(character:GetChildren()) do
		if v:IsA("Trail") then
			v:Destroy()
		end
	end

	wait()
end


remoteevent:FireServer()

which also checks it like that and worked (this script doesnt do what i need tho, it starts the trails the moment the player speeds up and not from speed 50, it also never shuts off when slowing down.)

i will also mention, this is the remoteevent, the particles are currently broken and i dont know why but thats not important right now, located in a server side script in ServerScriptService:

local remoteevent = game.ReplicatedStorage.RemoteEvent
local trail = game.ServerStorage.Trail
local particle = game.ServerStorage.ParticleEmitter

remoteevent.OnServerEvent:Connect(function(player)
	local character = player.Character

	-- Trail 1
	local trail1 = trail:Clone()
	trail1.Parent = character
	local attachment0 = Instance.new("Attachment", character.Head)
	attachment0.Name = "TrailAttachment0"
	local attachment1 = Instance.new("Attachment", character.UpperTorso)
	attachment1.Name = "TrailAttachment1"

	trail1.Attachment0 = attachment0
	trail1.Attachment1 = attachment1

	-- Trail 2
	local trail2 = trail:Clone()
	trail2.Parent = character
	local attachment2 = Instance.new("Attachment", character.RightUpperArm)
	attachment2.Name = "TrailAttachment2"
	local attachment3 = Instance.new("Attachment", character.RightLowerArm)
	attachment3.Name = "TrailAttachment3"

	trail2.Attachment0 = attachment2
	trail2.Attachment1 = attachment3

	-- Trail 3
	local trail3 = trail:Clone()
	trail3.Parent = character
	local attachment4 = Instance.new("Attachment", character.RightLowerArm)
	attachment2.Name = "TrailAttachment4"
	local attachment5 = Instance.new("Attachment", character.RightHand)
	attachment3.Name = "TrailAttachment5"

	trail3.Attachment0 = attachment4
	trail3.Attachment1 = attachment5

	-- Trail 4
	local trail4 = trail:Clone()
	trail4.Parent = character
	local attachment6 = Instance.new("Attachment", character.LeftUpperArm)
	attachment6.Name = "TrailAttachment4"
	local attachment7 = Instance.new("Attachment", character.LeftLowerArm)
	attachment7.Name = "TrailAttachment5"

	trail4.Attachment0 = attachment6
	trail4.Attachment1 = attachment7

	-- Trail 5
	local trail5 = trail:Clone()
	trail5.Parent = character
	local attachment8 = Instance.new("Attachment", character.LeftLowerArm)
	attachment6.Name = "TrailAttachment4"
	local attachment9 = Instance.new("Attachment", character.LeftHand)
	attachment7.Name = "TrailAttachment5"

	trail5.Attachment0 = attachment8
	trail5.Attachment1 = attachment9
	
	-- Trail 6
	local trail6 = trail:Clone()
	trail6.Parent = character
	local attachment10 = Instance.new("Attachment", character.LowerTorso)
	attachment6.Name = "TrailAttachment4"
	local attachment11 = Instance.new("Attachment", character.UpperTorso)
	attachment7.Name = "TrailAttachment5"

	trail6.Attachment0 = attachment10
	trail6.Attachment1 = attachment11
	
	-- Trail 7
	local trail7 = trail:Clone()
	trail7.Parent = character
	local attachment12 = Instance.new("Attachment", character.UpperTorso)
	attachment6.Name = "TrailAttachment4"
	local attachment13 = Instance.new("Attachment", character.RightUpperArm
	)
	attachment7.Name = "TrailAttachment5"

	trail7.Attachment0 = attachment12
	trail7.Attachment1 = attachment13
	
	-- Trail 8
	local trail8 = trail:Clone()
	trail8.Parent = character
	local attachment14 = Instance.new("Attachment", character.UpperTorso)
	attachment6.Name = "TrailAttachment4"
	local attachment15 = Instance.new("Attachment", character.LeftUpperArm
	)
	attachment7.Name = "TrailAttachment5"

	trail8.Attachment0 = attachment14
	trail8.Attachment1 = attachment15
	
	-- Trail 9
	local trail9 = trail:Clone()
	trail9.Parent = character
	local attachment16 = Instance.new("Attachment", character.LeftUpperLeg)
	attachment6.Name = "TrailAttachment4"
	local attachment17 = Instance.new("Attachment", character.LeftLowerLeg
	)
	attachment7.Name = "TrailAttachment5"

	trail9.Attachment0 = attachment16
	trail9.Attachment1 = attachment17
	
	-- Trail 10
	local trail10 = trail:Clone()
	trail10.Parent = character
	local attachment18 = Instance.new("Attachment", character.LeftLowerLeg)
	attachment6.Name = "TrailAttachment4"
	local attachment19 = Instance.new("Attachment", character.LeftFoot
	)
	attachment7.Name = "TrailAttachment5"

	trail10.Attachment0 = attachment18
	trail10.Attachment1 = attachment19
	
	-- Trail 11
	local trail11 = trail:Clone()
	trail11.Parent = character
	local attachment20 = Instance.new("Attachment", character.RightUpperLeg)
	attachment6.Name = "TrailAttachment4"
	local attachment21 = Instance.new("Attachment", character.RightLowerLeg
	)
	attachment7.Name = "TrailAttachment5"

	trail11.Attachment0 = attachment20
	trail11.Attachment1 = attachment21
	
	-- Trail 12
	local trail12 = trail:Clone()
	trail12.Parent = character
	local attachment22 = Instance.new("Attachment", character.RightLowerLeg)
	attachment6.Name = "TrailAttachment4"
	local attachment23 = Instance.new("Attachment", character.RightFoot
	)
	attachment7.Name = "TrailAttachment5"

	trail12.Attachment0 = attachment22
	trail12.Attachment1 = attachment23
	
	-- Trail 13
	local trail13 = trail:Clone()
	trail13.Parent = character
	local attachment24 = Instance.new("Attachment", character.LeftHand)
	attachment6.Name = "TrailAttachment4"
	local attachment25 = Instance.new("Attachment", character.LeftUpperArm
	)
	attachment7.Name = "TrailAttachment5"

	trail13.Attachment0 = attachment24
	trail13.Attachment1 = attachment25
	
	-- Trail 14
	local trail14 = trail:Clone()
	trail14.Parent = character
	local attachment26 = Instance.new("Attachment", character.RightHand)
	attachment6.Name = "TrailAttachment4"
	local attachment27 = Instance.new("Attachment", character.RightUpperArm
	)
	attachment7.Name = "TrailAttachment5"

	trail14.Attachment0 = attachment26
	trail14.Attachment1 = attachment27
	
	-- Trail 15
	local trail15 = trail:Clone()
	trail15.Parent = character
	local attachment28 = Instance.new("Attachment", character.RightFoot)
	attachment6.Name = "TrailAttachment4"
	local attachment29 = Instance.new("Attachment", character.RightUpperLeg
	)
	attachment7.Name = "TrailAttachment5"

	trail15.Attachment0 = attachment28
	trail15.Attachment1 = attachment29
	
	-- Trail 16
	local trail16 = trail:Clone()
	trail16.Parent = character
	local attachment30 = Instance.new("Attachment", character.LeftFoot)
	attachment6.Name = "TrailAttachment4"
	local attachment31 = Instance.new("Attachment", character.LeftUpperLeg
	)
	attachment7.Name = "TrailAttachment5"

	trail16.Attachment0 = attachment30
	trail16.Attachment1 = attachment31
	
	-- Trail 17
	local trail17 = trail:Clone()
	trail17.Parent = character
	local attachment32 = Instance.new("Attachment", character.UpperTorso
	)
	attachment6.Name = "TrailAttachment4"
	local attachment33 = Instance.new("Attachment", character.RootPart
	)
	attachment7.Name = "TrailAttachment5"

	trail17.Attachment0 = attachment32
	trail17.Attachment1 = attachment33
	
	local particle2 = particle:Clone()
	particle2.Enabled = true
	particle2.Parent = character.UpperTorso
	
	local particle3 = particle:Clone()
	particle3.Enabled = true
	particle3.Parent = character.LowerTorso
	
	local particle4 = particle:Clone()
	particle4.Enabled = true
	particle4.Parent = character.LeftFoot
	
	local particle5 = particle:Clone()
	particle5.Enabled = true
	particle5.Parent = character.LeftLowerLeg
	
	local particle6 = particle:Clone()
	particle6.Enabled = true
	particle6.Parent = character.LeftUpperLeg
	
	
	local particle7 = particle:Clone()
	particle7.Enabled = true
	particle7.Parent = character.RightFoot
	
	local particle8 = particle:Clone()
	particle8.Enabled = true
	particle8.Parent = character.RightLowerLeg
	
	local particle9 = particle:Clone()
	particle9.Enabled = true
	particle9.Parent = character.RightUpperLeg
	
	local particle10 = particle:Clone()
	particle10.Enabled = true
	particle10.Parent = character.LeftHand
	
	local particle11 = particle:Clone()
	particle11.Enabled = true
	particle11.Parent = character.LeftLowerArm
	
	local particle12 = particle:Clone()
	particle12.Enabled = true
	particle12.Parent = character.LeftUpperArm
	
	local particle13 = particle:Clone()
	particle13.Enabled = true
	particle13.Parent = character.RightHand
	
	local particle14 = particle:Clone()
	particle14.Enabled = true
	particle14.Parent = character.RightLowerArm
	
	local particle15 = particle:Clone()
	particle15.Enabled = true
	particle15.Parent = character.RightUpperArm

end)

Okay. This script here listens for when the character walk speed is changed, and if it’s equal to or above 50 then it will enable the trails(you need to add that code part) else it will disable the trails.

local function OnWalkSpeedChanged()
   if (Humanoid.WalkSpeed >= 50) then
   	-- enable trails
   else
   	-- disable trails
   end
end

Humanoid:GetPropertyChangedSignal('WalkSpeed'):Connect(OnWalkspeedChanged)```

If the `GetPropertyChangedSignal` doesn't work, you can swap that for the loop and test it from there.

is this correct?

local function OnWalkSpeedChanged()
	if (humanoid.WalkSpeed >= 50) then
		for i,v in pairs(character:GetChildren()) do
			if v:IsA("Trail") then
				v.enabled = false
	else
				for i,v in pairs(character:GetChildren()) do
					if v:IsA("Trail") then
						v.enabled = true
	end
end

humanoid:GetPropertyChangedSignal('WalkSpeed'):Connect(OnWalkspeedChanged)

Have it around the other way, so the top statement enables and the bottom disables.

local function OnWalkSpeedChanged()
	if (humanoid.WalkSpeed >= 50) then -- the characters walkspeed is at or above 50, this is where we should enable the trails
		for i,v in pairs(character:GetChildren()) do
			if v:IsA("Trail") then
				v.enabled = true
			end
		end
	else -- characters walkspeed is below 50, this is where we disable the trails.
		for i,v in pairs(character:GetChildren()) do
			if v:IsA("Trail") then
				v.enabled = false
			end
		end
	end

end
humanoid:GetPropertyChangedSignal('WalkSpeed'):Connect(OnWalkspeedChanged)

i dont know which part was wrong but there arent any trails anymore, to be sure i got the enable and disable code from the original which worked to enable it

Oh. My capitalisation was incorrect.
Fix the function name and rename it to OnWalkspeedChanged

didnt seem to fix it

local function OnWalkSpeedChanged()
	if (humanoid.WalkSpeed >= 50) then -- the characters walkspeed is at or above 50, this is where we should enable the trails
		for i,v in pairs(character:GetChildren()) do
			if v:IsA("Trail") then
				v.enabled = true
			end
		end
	else -- characters walkspeed is below 50, this is where we disable the trails.
		for i,v in pairs(character:GetChildren()) do
			if v:IsA("Trail") then
				v.enabled = false
			end
		end
	end

end
humanoid:GetPropertyChangedSignal('WalkSpeed'):Connect(OnWalkSpeedChanged)

here is a demo project that will enable trails when the character is moving faster then 50 and disable them if its moving slower then 50
Trail.rbxl (38.5 KB)

and this is the script inside the demo

local runService = game:GetService("RunService")

-- get the character
local character = script.Parent
-- get the root part
local rootPart = character.HumanoidRootPart
-- set enabled to false by default
local enabled = false
-- create a empty table where we will save all the characters trails
local trails = {}

-- loop all descendants of the character and add any tails to the trails table
for i, child in character:GetDescendants() do
	if child.ClassName ~= "Trail" then continue end
	table.insert(trails, child)
end


runService.Heartbeat:Connect(function()
	-- get the characters speed
	local speed = rootPart.AssemblyLinearVelocity.Magnitude
	
	if speed < 50 then
		-- if the speed is less then 50 and enabled is already false then just exit the function and do nothing
		if enabled == false then return end
		enabled = false
	else
		-- if the speed is more or equal to 50 and enabled is already true then just exit the function and do nothing
		if enabled == true then return end
		enabled = true
	end
	-- enables or disable all trails
	for i, trail in trails do
		trail.Enabled = enabled
	end
	
	print("Trails:", enabled)
end)

Try moving the function into a loop instead. Like 5uphi is doing, using Heartbeat, or using his code instead either or should work.