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

Why does this script not work?

Asked by 9 years ago

This script is trying to find out how long the player has been standing on a brick Output:

118:15:14.254 - Workspace.Part.Script:7: attempt to perform arithmetic on global 'howLong' (a nil value)
218:15:14.254 - Stack Begin
318:15:14.255 - Script 'Workspace.Part.Script', Line 7
418:15:14.255 - Stack End
01while not script.Parent.Touched do
02    howLong = 0 --This is how long the player has been on it
03    print(howLong)
04end
05 
06while script.Parent.Touched do
07    howLong = howLong + 1
08    if howLong == 20 then
09        print("captured")
10    wait(1)
11end
12end
0
Your script doesn't work? Dominus113 0 — 9y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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.

01local touching = {}; --Who is touching?
02local howLong = {}; --Time stats
03 
04--Track Touch Length
05function 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
View all 40 lines...
Ad

Answer this question