How do you add a Wait() to magnitude just like Humanoid:MoveToFinished:Wait()

Hi im trying to make a waypoint system for my car and magnitude is the only i can use, how can i add a :Wait() to the magnitude like wait until the magnitude is below 6 studs and then print ‘‘hello world’’ so it knows it went below 6 studs.

local Magnitude = 999
repeat
   task.wait(.1)  
   Magnitude =  ... -- Recalculate magnitude here
until Magnitude < 6
print("Hello World")
1 Like

Doesn’t work, until just stops it.

what is your current code (complete code)


local char = script.Parent
local mob = script.Parent
local waypoints = workspace.DataFolder

while wait(0.03) do
	local waypoints = workspace.DataFolder
	local mag = (script.Parent.PrimaryPart.Position - script.Parent.Target.Value.Position).Magnitude * 1
	for waypoint=1, #waypoints:GetChildren() do
		script.Parent.Target.Value = (waypoints[waypoint])
	   
	end
	print('I finished.')
end

have you included the bit of code I told told you to add?

i tried, but it did nothing it just did one difference and it was that it stopped the script

can you include the code with the bit I provided?

You arent recalculating the magnitude like what he said.

I don’t understand how to recalcuate

Just set it again??

mag = (script.Parent.PrimaryPart.Position - script.Parent.Target.Value.Position).Magnitude
1 Like

You should not use repeat until inside of a loop. Try putting an if statement to see if the studs are below 6

while wait(.03) do
 -- variables here
    if mag > 6 then
        -- do your stuff here
    elseif mag < 6 then
        print("magnitude is below 6 studs")

Based on what they seem to be doing in their code, they might actually need the repeat thing. They are having it wait for the condition to be met before continuing with a for loop, which is then inside the while loop

It is not a bad practice to use repeat until, but his code is not nearly effective to use it
It can still work, but won’t be as efficient and can cause trouble


local char = script.Parent
local mob = script.Parent
local waypoints = workspace.DataFolder

while wait(0.03) do
	local waypoints = workspace.DataFolder
	local mag = (script.Parent.PrimaryPart.Position - script.Parent.Target.Value.Position).Magnitude * 1
	for waypoint=1, #waypoints:GetChildren() do
		if mag < 35 then
			script.Parent.Target.Value = (waypoints[waypoint])
		end
		
	end
	print('I finished.')
end

Doing something like this, just skips to 5.

local char = script.Parent
local mob = script.Parent
local waypoints = workspace.DataFolder

while task.wait(0.03) do
	local waypoints = workspace.DataFolder
	local mag = (script.Parent.PrimaryPart.Position - script.Parent.Target.Value.Position).Magnitude

	for waypoint=1, #waypoints:GetChildren() do
		script.Parent.Target.Value = (waypoints[waypoint])

		repeat task.wait()
			mag = (script.Parent.PrimaryPart.Position - script.Parent.Target.Value.Position).Magnitude
		until mag < 35
	end

	print('I finished.')
end

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