Trail on player should stop when going speed 16

the first and second and third script should make a trail on each body part of a player when they hold shift and go above 60 speed (like the flash.), although it starts the moment you press shift and not only from speed 60. and though its supposed to go away when going under a certain speed (or exactly 16), but it doesnt. (third script should do this)

Main script

	local player = game.Players.LocalPlayer
	local char = player.Character
	local hum = char.Humanoid
	local UserInputService = game:GetService("UserInputService")
	local speed = game.ReplicatedStorage.Speed.Value


	UserInputService.InputBegan:Connect(function(input, isTyping)
		if isTyping then return end
		if input.KeyCode == Enum.KeyCode.LeftShift then
			speed = true

			while speed == true do
				if hum.WalkSpeed == 250 then break end
				hum.WalkSpeed = hum.WalkSpeed + 1
				wait()
			end
		end
	end)

	UserInputService.InputEnded:Connect(function(input, isTyping)
		if isTyping then return end
		if input.KeyCode == Enum.KeyCode.LeftShift then
			speed = false
			repeat
				if speed == true then break end
				hum.WalkSpeed = hum.WalkSpeed - 1
				wait()
			until
			hum.WalkSpeed == 16
		end
	end)

Start trail

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

if humanoid.WalkSpeed == 60 then
	remoteevent:FireServer()
end

Make trail: (ServerScriptService)

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

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.HumanoidRootPart)
	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.RightHand)
	attachment3.Name = "TrailAttachment3"

	trail2.Attachment0 = attachment2
	trail2.Attachment1 = attachment3

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

	trail3.Attachment0 = attachment4
	trail3.Attachment1 = attachment5

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

	trail4.Attachment0 = attachment6
	trail4.Attachment1 = attachment7

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

	trail5.Attachment0 = attachment8
	trail5.Attachment1 = attachment9
end)

End trail (spoken part)

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()

location:
image

1 Like
while humanoid.WalkSpeed == 16 do

there are two issues with this:

  1. once humanoid.WalkSpeed is no longer equal to 16, the loop will end and never run again
  2. you are only checking if walkspeed is exactly equal to 16, you probably want to use <= instead

how do i make it happen again if it is back to sped up?

A small patch would be to change you loop to an infinite loop and add the WalkSpeed check as an if statement inside, for example

while true do
  if humand.WalkSpeed <= 16 then

  end
end

Also, you start the trail from a script with only

if humanoid.WalkSpeed == 60 then
	remoteevent:FireServer()
end

this gets checked once and then never again, this should also be a part of a loop (preferably the same loop as the check if WalkSpeed <= 16) and it also only checks if the WalkSpeed is exactly 60

It’s outside the scope of your question, but in the first script, you modify the WalkSpeed of the player from within a LocalScript

image
it becomes red underlined

Thats because that is an infinite loop, the underlined code will never run

so it should be while? and does it belong in a server side script?

is that supposed to happen? or not

It should be in a while loop if the check should be done repeatedly, which I assumed to be true. What is the goal of the firing of the remote event?

im not sure, its been long since i worked on this game, ill remove it and see what happens.

i deleted it and now the last 2 lines of code also underlined red.
image

The error message states you forgot to close the if statement, you need to close it

I tested it and the trails didnt show up,
i added

remoteevent:FireServer()

back and it still didnt work.

You should probably name the remote event to describe what it does, but I’m going to assume it creates the trails. You need one infinite loop with a check if speed has gone above 60 and a check if speed has gone below 16, when it has crossed the 60 threshold, fire the event (but make sure it doesn’t fire repeatedly)

do you have time to help? i have no idea how to fix it, i reverted it back to its original state, the main problem is actually just it not going away after slowing down

I would replace scripts 2 and 4 with one that detects any change, I would look at the Changed signal, maybe try something like

Humanoid.Changed(function(property)
  if property == "WalkSpeed" then
    -- check walk speed here
  end
end)
``

Could this be done by using
Character.Humanoid.MoveDirection.Magnitude?

1 Like

Character.Humanoid.MoveDirection.Magnitude will always be 1, I meant your code that checks is Humanoid.WalkSpeed is equal 16 or 60, that code should go in there

2 Likes