Why won't this print?

Hello!
I ran into a problem that I can’t quite put my finger on, I am unable to define this variable and/or print it. I can’t find an answer, any help is appreciated

-- located in started pack
-- is a local script
-- script.parent.parent is the player
while true do wait(1)
	local distance = math.sqrt(math.pow(math.max(script.Parent.Parent.RespawnLocation.Position.X,
		script.Parent.Parent.RespawnLocation.Position.Y) - math.min(script.Parent.Parent.RespawnLocation.Position.X,
			script.Parent.Parent.RespawnLocation.Position.Y),2) + math.pow(math.max(mouse.Hit.p.X,mouse.Hit.p.Y) - 
				math.min(mouse.Hit.p.X,mouse.Hit.p.Y),2))
	print(distance)
end

I think it would be better if you just do all the calculations in seperate lines. It’s jumbled up into one single variable and it’s hard to read them all.

1 Like

I will spread them out more, see if I see a miscalculation.

1 Like

Most probably, just start doing the the calculations one by one.

All spread out, easy to read, however it still does not print.

while true do wait(1)
	local a = math.max(script.Parent.Parent.RespawnLocation.Position.X,
		script.Parent.Parent.RespawnLocation.Position.Y)
	local b = math.min(script.Parent.Parent.RespawnLocation.Position.X,
		script.Parent.Parent.RespawnLocation.Position.Y)
	
	local c = math.max(mouse.Hit.p.X,mouse.Hit.p.Y)
	local d = math.min(mouse.Hit.p.X,mouse.Hit.p.Y)
	
	local distance = math.sqrt(math.pow(a - b) + math.pow(c-d))
	
	print(distance)
end

math.pow() needs 2 arguments, you only supplied one for each.

1 Like

I assume you’re trying to use the pythagoras theorem, in this case, the second argument of both power functions should be 2

local distance = math.sqrt(math.pow(a - b, 2) + math.pow(c-d, 2))

1 Like

Thank you for pointing that out, I just fixed it and it’s not printing. I’m starting to think that it’s something else, other than the equation.

I figured it out, you can’t put a while true loop in a script containing other functions.

yes you can, spawn it on another thread with

task.spawn(function()
    while task.wait(1) do
	    local a = math.max(script.Parent.Parent.RespawnLocation.Position.X,
		    script.Parent.Parent.RespawnLocation.Position.Y)
	    local b = math.min(script.Parent.Parent.RespawnLocation.Position.X,
		    script.Parent.Parent.RespawnLocation.Position.Y)
	
	    local c = math.max(mouse.Hit.p.X,mouse.Hit.p.Y)
	    local d = math.min(mouse.Hit.p.X,mouse.Hit.p.Y)
	
	    local distance = math.sqrt(math.pow(a - b) + math.pow(c-d))
	
	   print(distance)
    end
end)

Oh thanks! I didn’t know that.