Zattex
|
  |
| Joined: 12 Mar 2010 |
| Total Posts: 65 |
|
|
| 13 Oct 2015 07:21 PM |
...basically detect a players location and if their location is less than a specific distance from an object, say, a light for instance, that light turns on; then when you walk out of that radius, the light turns back off?
I had a script that when you walk inside of a transparent, non-collidable brick, it would activate, but I feel like there's a better way to do this, and I would like to learn. |
|
|
| Report Abuse |
|
|
|
| 13 Oct 2015 07:37 PM |
distance = (part1.Position - part2.Position).magnitude
Or Region3. |
|
|
| Report Abuse |
|
|
|
| 13 Oct 2015 07:48 PM |
There's a lot of inefficient ways of doing that.
such as using a while true loop to determine the distance of all the characters in the game from the object...
you can reduce the lag by checking less often or by having a proximity switch (block) to turn on the loop.
so when a player touches the prox. block, the loop will run for x amount of time until having to be prox'd again.
it's all up to you. |
|
|
| Report Abuse |
|
|
Zattex
|
  |
| Joined: 12 Mar 2010 |
| Total Posts: 65 |
|
|
| 13 Oct 2015 09:17 PM |
| Sorry, I'm new to scripting, so bare with me here. How exactly do I get the player's position? |
|
|
| Report Abuse |
|
|
|
| 13 Oct 2015 09:36 PM |
for i,v in ipairs(game.Players:getChildren())do if v.Character then if v.Character.Torso then position=v.Character.Torso.Position print(position) end end end |
|
|
| Report Abuse |
|
|
Zattex
|
  |
| Joined: 12 Mar 2010 |
| Total Posts: 65 |
|
|
| 14 Oct 2015 10:46 PM |
Hmm... I saw somewhere about using a "Changed" event on a player's torso that would test for close objects every time the position changes...
How would I go about doing that? |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2015 10:53 PM |
Character.Torso.Changed:connect()
thats assuming you have the character. |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2015 10:55 PM |
Try hooking up to a part's .Touched event. Position it on the entrance or somewhere. If you want to hook up to the .Changed event then you'd do something like this:
torso.Changed:connect(function() if (torso.Position -light.Position).magnitude <= range than --code end end) |
|
|
| Report Abuse |
|
|
Zattex
|
  |
| Joined: 12 Mar 2010 |
| Total Posts: 65 |
|
|
| 14 Oct 2015 11:25 PM |
So I wrote a script that doesn't work so I'm still honestly a little confused here... Maybe you can show me why what I wrote is stupid? Here's the script:
local light = game.Workspace.Light
for i,v in ipairs(game.Players:GetPlayers()) do local torso = v.Character.Torso local charPos = torso.Position local h = v.Character:WaitForChild("Humanoid") if torso and h then torso.Changed:connect(function() local distance = (light.Position - charPos).magnitude if distance <= 20 then light.PointLight.Enabled = true else light.PointLight.Enabled = false end end) end end
Also, I had another question... What exactly does then "for i,v in ipairs(game.Players:GetPlayers()) do" line mean? Because I knew about for loops where you could write something like "for i = 1,10 do" and it would loop ten times, so the fact that something like, "for i,v in ipairs(game.Players:GetPlayers()) do" confuses me to no end! |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2015 11:28 PM |
................................
Region3?
-The [Guy] |
|
|
| Report Abuse |
|
|
Zattex
|
  |
| Joined: 12 Mar 2010 |
| Total Posts: 65 |
|
|
| 14 Oct 2015 11:29 PM |
@BusyCityGuy
Wow, thanks!
......
T-T |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2015 11:35 PM |
Haha the more you know.
-The [Guy] |
|
|
| Report Abuse |
|
|
Zattex
|
  |
| Joined: 12 Mar 2010 |
| Total Posts: 65 |
|
| |
|
|
| 14 Oct 2015 11:45 PM |
No seriously though, look into Region3, this is very likely what you are looking for.
-The [Guy] |
|
|
| Report Abuse |
|
|
Zattex
|
  |
| Joined: 12 Mar 2010 |
| Total Posts: 65 |
|
|
| 15 Oct 2015 08:29 PM |
Bump
Still would like a reply for my post 6 posts above... |
|
|
| Report Abuse |
|
|
|
| 15 Oct 2015 08:40 PM |
@Zattex, about the i,v loop, think of it this way:
for i,v in pairs (group of things) do
i is your index, so just like your x= for x=1,10 your i value would be what iteration you were on.
v is your object currently being looped with from the group
(the group is a table of objects usually.)
in pairs is just saying take the table of objects and their indexes and loop in order using them. |
|
|
| Report Abuse |
|
|
Zattex
|
  |
| Joined: 12 Mar 2010 |
| Total Posts: 65 |
|
|
| 15 Oct 2015 08:46 PM |
Ooh.. Okay, I get it. Thanks!
And is it possible to explain why the script doesn't work? |
|
|
| Report Abuse |
|
|
|
| 15 Oct 2015 10:12 PM |
Well, whats your output?
do you have an error? |
|
|
| Report Abuse |
|
|
Zattex
|
  |
| Joined: 12 Mar 2010 |
| Total Posts: 65 |
|
|
| 15 Oct 2015 10:13 PM |
| It appears absolutely nothing happens in the output. |
|
|
| Report Abuse |
|
|
|
| 15 Oct 2015 10:22 PM |
| Then torso:changed won't work most likely. |
|
|
| Report Abuse |
|
|
|
| 15 Oct 2015 10:27 PM |
http://www.roblox.com/Forum/ShowPost.aspx?PostID=46988918
"part.Changed:connect(funtion(Position) The argument is the property."
~eXoguti093 |
|
|
| Report Abuse |
|
|