generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Check to see when player is not touching part anymore

Previous Thread :: Next Thread 
jager72 is online. 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 is not online. Quenty
Joined: 03 Sep 2009
Total Posts: 9316
13 Mar 2017 03:27 PM
script.parent.TouchEnded:connect(function(Part)

end)
Report Abuse
Lecturous is not online. 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 is not online. Fredfishy
Joined: 21 Mar 2009
Total Posts: 4197
13 Mar 2017 03:28 PM
Connect a function to the .TouchEnded event.
Report Abuse
jager72 is online. 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 is not online. 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 is online. 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
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
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 is online. 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
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
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 is not online. 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 is not online. 00doggie
Joined: 30 Jul 2009
Total Posts: 337
13 Mar 2017 05:41 PM
tags ruined my solution :/
Report Abuse
00doggie is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image