Making magnitude more effective?

while wait(0.1) do
if script.Allow.Value == true then
if (game.Players.LocalPlayer.Character.Torso.Position - game.Workspace.Door.Position).magnitude <=10 then
game.Workspace.CurrentCamera.CameraType = "Scriptable"
game.Workspace.CurrentCamera.CFrame = game.Workspace.CameraFolder.Cam2.CFrame
print("Near Door!")
elseif (game.Players.LocalPlayer.Character.Torso.Position - game.Workspace.Door.Position).magnitude >10 then
game.Workspace.CurrentCamera.CameraType = "Scriptable"
game.Workspace.CurrentCamera.CFrame = game.Workspace.CameraFolder.Cam1.CFrame
print("Far from Door!")
end	
end	
end

So I’m trying to make this script detect if the player is far or near the door then it changes the camera view, but my problem is I don’t think using a loop will be efficient for this.

Do you guys have any ideas for this script to more efficient and and not make it’s activity rate high?

2 Likes

You could use a Region3 but that might end up eating more memory, however it could also cut activity rate if used correctly. What you’re doing right now isn’t exactly inefficient but there are certainly better methods, however I lack a fair deal of knowledge in what eats up the most memory, so maybe do some testing of your own with each method and come up with what works best using the statistics window.

You can use the .Changed function of RBXScriptSignal and only check the magnitude when the player’s position changes, which would probably be less costly.

You can use the :FindPartsInRegion3WithWhiteList() method and recode it so that if the player is within the Region3 it will be considered as near the door.

At the end of the day you aren’t going to eat up a huge portion of your game’s memory by running this loop so as long as it doesn’t bother you that there are probably better ways, it shouldn’t really matter.

1 Like

A loop is probably your best bet. The physics engine doesn’t have any nice callbacks for “these two things are close”, and even if it did, it wouldn’t provide the control you need for defining “close”.

I would update your wait statement so it waits based on the player’s current distance from the door.

MAX_WALKSPEED=20
while true do
    local distance = (HumanoidRootPart.Position - Door.Position).magnitude;
    if distance <= 10 then
        --player is close to door.
    elseif distance > 10 then
        --player is far from door.
    end
    --Wait .1 seconds if the player is right on the fringe.
    --Wait more than that if the player is far from the door.
    wait(math.clamp((distance - 10) / MAX_WALKSPEED, .1, 60));
end

This will increase the amount of time your thread is spent sleeping and should help keep runtime costs low.

17 Likes

Yeah I agree with @blobbyblob. I’d be heavily concerned if checking the magnitude of something every 0.1 seconds caused any performance issues. In fact, you could probably check 10,000 of them every frame lol.

7 Likes

If the distance is only 10 I’d used an invisible part that’s got a radius of 10 studs and just listen to .Touched/TouchedEnded. That way you can take advantage of Roblox’s quadtree structure and C code.

5 Likes

That’s true! The code ends up being cleaner and clearer too. I do this for automatic doors to detect when to open/close them

3 Likes