Can't get magnitude

so, im making a waypoint system for my open-world game but the distance meter won’t work, here’s how it looks like:
image
(screenshot made while testing)
the Vector3.Magnitude does not work, AT ALL.
LocalScript:

while wait() do
	script.Parent.Text = (game.Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid").RootPart.Position - script.Parent.Parent.Parent.Position).Magnitude
end

first attempt was to use GetBoundingBox().Position, but didn’t work.
again, this is a LocalScript so i don’t know if it’s even supposed to work
if anyone knows the solution, please tell me.

game.Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid").RootPart.Position

should be

game.Players.LocalPlayer.Character.HumanoidRootPart

also replace wait() with task.wait()

still not it, but i used wait just for performance
thanks tho

is the localscript placed under the player’s character or the player?
also wait is way slower than task.wait

I notice you are using script.Parent.Parent.Parent, does this mean your LocalScript is a descendant of Workspace?

well yeah, but at first its in replicatedStorage

its in the distance gui(BillboardGui.TextLabel

localscripts wont run in workspace, try placing it in starterplayercharacter or starterplayerscripts
this does mean that youll have to change script.Parent.Parent.Parent though

that gives me an idea (characterminimumlimitaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)

What you might prefer is to place the BillboardGui in the player’s PlayerGui and assign the Adornee to the part. You can then reference the part via the Adornee property

also some posts say magnitude doesnt work in localscripts, but ill try

magnitude isnt exclusive to local or server scripts you should be fine

hold on, the player can create multiple waypoints

well, lua gave me an idea so idk bout that

This isn’t true but the player’s Character and Character.HumanoidRootPart can potentially be nil at runtime, so ensure they exist before usage!

Also, busy while loops can be expensive - try using a RunService event such as PostSimulation (just after the character’s velocity has been applied)

alright, i’ll try using a for loop

I don’t recommend this either. Any kind of loop repeatedly calling yielding function can be detrimental, always prefer events!

an updated script in starterplayerscripts:

game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
game:GetService("RunService").RenderStepped:Connect(function()
	if char ~= nil then
			for _,v in pairs(workspace.WaypointStorage:GetChildren()) do
				local label = v:FindFirstChild("Distance").TextLabel
				if label and v:IsA("BasePart") then
					local mag = (char:FindFirstChildOfClass("Humanoid").RootPart.Position - v.Position).magnitude
					label.Text = math.ceil(mag)
				end
			end
	end
end)
end)

and now it stops working upon respawn, and the output is empty

alright, i parented it to startercharacterscripts, modified it and it works

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