This script is trying to find out how long the player has been standing on a brick Output:
1 | 18 : 15 : 14.254 - Workspace.Part.Script: 7 : attempt to perform arithmetic on global 'howLong' (a nil value) |
2 | 18 : 15 : 14.254 - Stack Begin |
3 | 18 : 15 : 14.255 - Script 'Workspace.Part.Script' , Line 7 |
4 | 18 : 15 : 14.255 - Stack End |
01 | while not script.Parent.Touched do |
02 | howLong = 0 --This is how long the player has been on it |
03 | print (howLong) |
04 | end |
05 |
06 | while script.Parent.Touched do |
07 | howLong = howLong + 1 |
08 | if howLong = = 20 then |
09 | print ( "captured" ) |
10 | wait( 1 ) |
11 | end |
12 | end |
Touched
is not a Property of the Part object, it is an event. Events need to be setup with connection statements, tied to functions filled with code that you require to run whenever the event is fired.
Also, you should store the 'howLong' property in either the Player object or in separate values in a table since multiple people could be touching the part.
01 | local touching = { } ; --Who is touching? |
02 | local howLong = { } ; --Time stats |
03 |
04 | --Track Touch Length |
05 | function track(player) |
06 | --Every second |
07 | while wait( 1 ) do |
08 | --If the player is still touching |
09 | if touching [ player ] then |
10 | --Then increment the stats |
11 | howLong [ player ] = howLong [ player ] + 1 or 1 ; |
12 | print (player.. ": " ..howlong [ player ] ); |
13 | else |
14 | --Otherwise, break the loop |
15 | break |