End loop when part destroyed. ServerScriptService

I want LinearVelocity when a key has been held by a user, so I have 2 scripts, one, which adds linear velocity to the character when InputBegan, and another which destroys it when InputEnded.

I have made a script that will change the velocity depending on the character CFrames (needs improvement, accepting suggestions)

This is a loop in a server (might be inefficient, accepting suggestions), and I want the loop to end when the LinearVelocity has been destroyed by another script.

I tested this by putting print(), but it seems like the loop is still running because it is still printing.
How can I end the loop when LinearVelocity has been destroyed?

Thanks!

game.ReplicatedStorage.add.OnServerEvent:Connect(function(p)
	local char = p.Character or p.CharacterAdded:Wait()
	local HRP = char.HumanoidRootPart

	local veloc = Instance.new("LinearVelocity",char)
	veloc.Attachment0 = char.HumanoidRootPart.RootRigAttachment
	veloc.MaxForce = 5000
	while veloc do
		veloc.VectorVelocity = (char:GetPivot().LookVector*10 + char:GetPivot().UpVector)*10
		print("a")
		task.wait(0.1)
	end

end)```
1 Like

Try finding the part instead:

while char:FindFirstChild("LinearVelocity") do
-- code here
end

Thank you! I know this isn’t the topic, but do you see any other problems with my script?

Thanks!

1 Like

Sorry for late reply but i don’t see any big problems with your script. Seems fine to me.

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