How to check player distance between object every second

This is the 4 time I have posted, here is my question:

Let’s say I have a part called PartA, if the distance between a player and the PartA is < 100 studs, then it will display an image on the client side. Therefore, I should add a tag for PartA and use CollectionService to check each Part with a tag and check the player distance between parts for each part every second or there is a better method?

Also, when the image is displayed, how to let the image keep facing the player camera?

Thank you

You could do


if (HumanoidRootPart.Position - PartA.Position).Magnitude < 100 then
      -- Code
end

2 Likes

You do not need collection service here unless you have more parts which are the same as part A. Just try this (local script in starterGui)

local PartA = workspace.PartA
local char = game.Players.LocalPlayer
local image = — Put your image here, I’m assuming it’s a image label
game[“Run Service”].RenderStepped:Connect(function()
   if (char.PrimaryPart.Position - PartA.Position).Magnitude < 100 then
      image.Visible = true
  else
      image.Visible = false
end)

If you have any more questions, reply to this post to ask me

3 Likes

game[“Run Service”].RenderStepped Does not run every second

Why should it be ran every second instead?

read topic name.

How to check player distance between object every second

Sorry for unclear topic but every frame is ok already, thank you.

But will game:GetService("RunService").RenderStepped: cause lag or something like that? Or while task.wait() will be worse?
Thank you

(I used BillboardGui for letting the image keep facing the camera already.)

Generally render stepped causes less lag then while loops. I’ll supply some numbers to show you
wait() waits 1 ms which is 0.001s
the highest frame rate rn is 360 I think so 1/360 = 0.003s (3 d.p)
So RunService causes less lag

ok, thank you so much for helping!

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