Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Alternative to constant loops?

Asked by 10 years ago

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

Log in to vote
0
Answered by 10 years ago

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!

Ad

Answer this question