Value of distance not updating

I tried to make the script print the distance between the player and the part; It only prints out one value.
Here’s the code:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	local humanoidRoot = char:WaitForChild("HumanoidRootPart")
	
	local partfolder = workspace.Folder
	
	for _,v in pairs(partfolder:GetChildren()) do
		
		while wait() do
			
			local distance = (humanoidRoot.Position - v.Position).magnitude
		
			while wait(1) do
			
			print("You are " .. distance .. " studs away from the part!")
			
			end
		end
	end
end)

Here’s a video of it not working:


If you can help, please do. All help is appreciated!

Try this:

for _,v in pairs(partfolder:GetChildren()) do
		task.spawn(function()
		while task.wait(1) do
			local distance = (humanoidRoot.Position - v.Position).magnitude
			print("You are " .. (math.floor(distance*100/100)) .. " studs away from the part!")
		end
end)
	end

(math.floor(distance*100/100)) is just so instead of you having a number like 24.549684359, you have 24.54

Thank you, it worked. Just one question, what does task.spawn do?

To put it simply: Accepts a thread and fire’s it immediately,

it also makes it so your code doesn’t yield, like coroutines

Ok, thank you. That’s going to be helpful.

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