Why doesn't my code work?

im trying to make a trail and when you move it creates parts under you, i tried making it so that each part has a timer and while you move it goes down and when its at 0 it disapears, i made this code and it won’t work, is there a problem with it?

local plr = game.Players.LocalPlayer
local timer = 5
while true do
	wait(0.1)
	if plr.Character.Humanoid.MoveDirection.Magnitude == 0 then
		
	else
		timer -= 0.1
	end
	if timer <= 0 then
		script.Parent:Destroy()
	end
end
1 Like

I see several potential problems. First off, LocalPlayer will return nil if run on a server script, so if this isn’t a local script, you’ll effectively be checking nil against 0, making it so that the timer never runs down (ie. “timer -= 0.1” will never run because “plr.Character.Humanoid.MoveDirection.Magnitude == 0” is either always true or throwing an error that you didn’t see/aren’t mentioning). The fix to this would be to move your code onto a local script.

Secondly, script.Parent:Destroy() would only destroy the part if the script is parented to the part. I imagine that running this on a local script wouldn’t make it update on the server. If that’s a problem (eg. your game is multiplayer), you might have to rethink how you make these trails altogether.

On a final note that might be useful if the second point applies to you, you should probably be using the trail object built into Studio.

1 Like

Why are you creating a new topic about a topic that you have just created?

What do you mean it won’t work? What exactly isn’t working? Is the trail part not being destroyed? Are you getting any errors or warnings in your console?

Also, are you doing this in a local or a server script. Judging by this line of code here:

I assume you are doing this in a local script, which won’t work because the trail part will only be deleted on the client but not on the server.

i think it might be the server problem with deleting because they never go away

it is on a local script. (more letters)

They already answered why…

You should do the trail part deletion process on the server, NOT the client. Any changes made or done by the client will not be replicated across the server-client boundary unless through the means of RemoteEvents and similar instances.

I am not sure if this will fit exactly what you are trying to do but the main idea is that each part needs its own timer instead of sharing one!!!


The problem is that you only have one timer for everything. That means as soon as you move once it starts counting down and destroys the script parent instead of each part having its own timer!

You will want to make the part and give it its own countdown when you create it.

local plr = game.Players.LocalPlayer

plr.CharacterAdded:Connect(function(char)
	local hum = char:WaitForChild("Humanoid")
	
	hum.Running:Connect(function(speed)
		if speed > 0 then
			local part = Instance.new("Part")
			part.Size = Vector3.new(2, 1, 2)
			part.Anchored = true
			part.CFrame = char.PrimaryPart.CFrame * CFrame.new(0, -3, 0)
			part.Parent = workspace
			
			task.spawn(function()
				local timer = 5
				while timer > 0 do
					task.wait(0.1)
					timer -= 0.1
				end
				part:Destroy()
			end)
		end
	end)
end)

the script IS in each part though, which means each part has its own timer, i think i forgot to say that.

If the script is already inside each part, then yeah, each part will technically have its own timer running. The bigger issue though is where that script is running from.

Because your using a LocalScript the part is only being destroyed on your client… the server (and other players) never see it get deleted. Thats why it looks like they are “never going away.”

To fix this, the destruction needs to be handled on the server instead of only the client.

  • Move the timer/destroy logic into a server Script inside the part, or

OR

  • Have your LocalScript fire a RemoteEvent to the server, so the server creates the part and also cleans it up after the timer runs out!

That way, the parts will actually disappear for everyone not just you!

2 Likes

Are there any Errors in the Output?

If it doesn’t disappear, the script either ends early because of an error, deletes itself before deleting the parent or the condition for the timer to go down is never met so that timer <= 0 is never true.

Try printing plr.Character.Humanoid.MoveDirection.Magnitude and timer to see how the valued change.

Both solutions MRX_perte mentioned make sense, but I think it would force you (ie. TheRealBoiAnimates) to give up the logic involving the local player (ie. “plr.Character.Humanoid.MoveDirection.Magnitude == 0”), since the timer would then be on a server-side script, which can’t get local player. If you want to go with MRX_perte’s solution and still keep the player-movement logic, I’d listen to the humanoid root part’s AssemblyLinearVelocity property and execute the “plr.Character.Humanoid.MoveDirection.Magnitude == 0” logic when it is close to 0.

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