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

How can I detect if a player is near (as an event), possible? Without using while true do?

Asked by 10 years ago

If I use while true do, wouldn't it create lag for unnecessary checking every-time? Can someone point me in the right direction? I just need to know what I should do from this point on.

0
If a player is near what? I can help with this, but be more specific. yoshiegg6 176 — 10y
0
Just put in a wait longer than wait(0) and it won't cause lag. Perci1 4988 — 10y
0
If it's just a brick its simple, if its a model its more complicated. yoshiegg6 176 — 10y

3 answers

Log in to vote
0
Answered by 10 years ago

I don't believe there's an event for this, but you can use a loop and if creating a new thread is necessary, coroutines. You can use magnitude to check the distance between two specified parts. For example:

1local base = workspace.base
2local part = workspace.part
3coroutine.resume(coroutine.create(function()
4while wait(1) do
5print (part.Position - base.Position).magnitude
6end
7end))
8print("bork")
0
This seems good for an answer. Thanks, I suppose. It wouldn't create unnecessary lag would it, hm... If you're checking constantly if a player is near something? I hope it doesn't. Arithmeticity 167 — 10y
0
It shouldn't create toom much. This is a popular method. aquathorn321 858 — 10y
Ad
Log in to vote
0
Answered by
yoshiegg6 176
10 years ago

You can make an invisible non can collide-able cylinder/sphere and when a player touches it with the touched event you can change a variable to true. When the player leaves the range of nearness (invisible cylinder) you can use the touchended event and change a variable to false. Ex

01--Put this script in an invisible non can collideable sphere that surrounds the object that you want to know if the player is near
02 
03PlayerIsNear = false
04 
05script.Parent.Touched:connect(function()
06PlayerIsNear = true
07end)
08 
09script.Parent.Touchended:connect(function()
10PlayerIsNear = false
11end)
12 
13if PlayerIsNear == true then
14--Do whatever you want here

This creates a lot less lag then a loop

Log in to vote
0
Answered by
LevelKap 114
10 years ago

There isn't an event, but if you want an alternative method, you can use magnitudes to detect how close the player is to an object. From there you can determine what you want to do with the script. I used this to create a type of radar/ way to find players near you.

Answer this question