jager72
|
  |
| Joined: 14 Aug 2010 |
| Total Posts: 11034 |
|
|
| 13 Mar 2017 03:26 PM |
I have a part that turns on a light when the player touches a part.
local light = script.Parent.Parent.Light.PointLight local on = script.Parent.Parent.On
function onTouched(hit) local human = hit.Parent:findFirstChild("Humanoid") if (human ~= nil) and on.Value == false then on.Value = true for i = 1, 50 do light.Brightness = light.Brightness + 0.02 wait(0.01) end end end
script.Parent.Touched:connect(onTouched)
What should I add to this so that the light turns off when the player is not touching the part anymore? |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 13 Mar 2017 03:27 PM |
script.parent.TouchEnded:connect(function(Part)
end) |
|
|
| Report Abuse |
|
|
Lecturous
|
  |
| Joined: 17 Aug 2013 |
| Total Posts: 1096 |
|
|
| 13 Mar 2017 03:28 PM |
| http://wiki.roblox.com/index.php?title=API:Class/BasePart/TouchEnded |
|
|
| Report Abuse |
|
|
Fredfishy
|
  |
| Joined: 21 Mar 2009 |
| Total Posts: 4197 |
|
|
| 13 Mar 2017 03:28 PM |
| Connect a function to the .TouchEnded event. |
|
|
| Report Abuse |
|
|
jager72
|
  |
| Joined: 14 Aug 2010 |
| Total Posts: 11034 |
|
|
| 13 Mar 2017 03:48 PM |
| Ok, now it does turn off after walking off. However, how could I make it so that the brightness goes to 0 when the player is not touching the part rather than going down by 0.02 50 times? |
|
|
| Report Abuse |
|
|
isc88_v4
|
  |
| Joined: 24 Feb 2016 |
| Total Posts: 683 |
|
|
| 13 Mar 2017 03:51 PM |
use a debounce.
debounce = false
function onTouched(hit) if debounce == false then debounce = true --put your code here! wait(1) debounce = false end end
script.Parent.TouchEnded:connect(onTouched) --Should only activate once every touch ended
|
|
|
| Report Abuse |
|
|
jager72
|
  |
| Joined: 14 Aug 2010 |
| Total Posts: 11034 |
|
|
| 13 Mar 2017 04:00 PM |
I'm trying to stop lowering the brightness once it reaches 0. Why isn't this working?
repeat light.Brightness = light.Brightness - 0.02 wait(0.01) until light.Brightness = 0 |
|
|
| Report Abuse |
|
|
|
| 13 Mar 2017 04:03 PM |
Because of floating point imprecision. And generally repeat loops don't really have a purpose. Use a for loop.
|
|
|
| Report Abuse |
|
|
jager72
|
  |
| Joined: 14 Aug 2010 |
| Total Posts: 11034 |
|
|
| 13 Mar 2017 04:11 PM |
I did initially have a for loop
for i = 1, 50 do light.Brightness = light.Brightness - 0.02 wait(0.01) end
but the problem was that if the player didn't step on the part for long enough to make the brightness 1, then the brightness would go into the negatives after they stopped touching the part. |
|
|
| Report Abuse |
|
|
|
| 13 Mar 2017 04:39 PM |
Then either use a repeat loop like you were using or use the conditional part of a while loop.
Instead of checking for equality, use the `<=` operator.
|
|
|
| Report Abuse |
|
|
00doggie
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 337 |
|
|
| 13 Mar 2017 05:41 PM |
| for i = 1, 50 do if light.Brightness >= #### #### ################ # ################ # #### ### wait(0.01) end |
|
|
| Report Abuse |
|
|
00doggie
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 337 |
|
|
| 13 Mar 2017 05:41 PM |
| tags ruined my solution :/ |
|
|
| Report Abuse |
|
|
00doggie
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 337 |
|
|
| 13 Mar 2017 05:51 PM |
for i = 1, 50 do if light.Brightness >= 0.02 then light.Brightness = light.Brightness - 0.02 wait(0.01) end end |
|
|
| Report Abuse |
|
|