Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why isnt magnitude working correctly?

So, if your distance is not 10 then the billboard gui will be enabled but if you are in 10 distance then the gui will get removed but for some dumb reason it dosent do that so insted it are giving me an error why?

Script.

local door = game.workspace.Gandalf.head

while true do
	for i,v in pairs(game.Players:GetChildren()) do
		if (v.Character.Torso.Position - door.Position).magnitude <= 10 then
			if door.TrackGui then
				door.TrackGui:Destroy()
			end
		end
	end
	
	for i,v in pairs(game.Players:GetChildren()) do
		if (v.Character.Torso.Position - door.Position).magnitude >= 10 then
			game.ServerStorage.TrackGui:Clone().Parent = door
		end
	end
wait()
end

Error.

23:39:19.341 - TrackGui is not a valid member of Part
23:39:19.342 - Stack Begin
23:39:19.342 - Script 'Workspace.Gandalf.Head.Script', Line 6

1 answer

You need to use FindFirstChild, as line 6 checks if trackgui exists, but its not checking, its assuming it already exists.

if door:FindFirstChild("TrackGui") then

2 other mistakes, when creating the new billboard, you dont check if one already exists. Also, don't you need to set adornee of a billboard?

Anyway, your code is very very very inefficient. Your doing the loop on wait(), that is way too fast and is not needed. Also, creating and destroying objects is very inefficient, rather create the billboard and change the Enabled property of the billboardgui

local door = workspace.Gandalf.head

local Billboard = game.ServerStorage.TrackGui:Clone()
Billboard.Parent = door
Billboard.Adornee = door

while wait(0.5) do
	local InView = false
	for _, v in pairs(game.Players:GetChildren()) do
		if (v.Character.Torso.Position - door.Position).magnitude <= 10 then
			InView = true
		end
	end
	Billboard.Enabled = InView
end

This discussion is preserved as it was. New answers, comments, and votes are closed.