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

Alternative to constant loops?

Say I want to constantly check the distance between any given character and an object.

Using a while loop isn't that effective, and functions would fire just as much.

What's the most efficient way?

1 answer

Usually for something like that you would use an event to keep it efficient. For this specific situation I would recommend the 'Changed' event for the Part instance. It would go a little something like this..

local char = game.Workspace.Player1
local part = game.Workspace.Object
local mag = nil

function onChange(property)
	if property == "Position" then
		mag = (char.Torso.Position - Object.Position).magnitude
	end
end

Object.Changed:connect(onChange)
char.Torso.Changed:connect(onChange)

Hope that helps!

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