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

Why is this script erroring? [closed]

Asked by
AZDev 590 Moderation Voter
8 years ago

It is so simple which means the answer is probably obvious.

Here is my script.

script.Parent.Touched:connect(function(hit)
    if hit.Name == "Copper" then
        workspace.Tycoon.Collector.Cash.Value = workspace.Tycoon.Collector.Cash.Value + 2
    end
end)

This is my error: Collector is not a valid member of Model

I realize the code can be shortened. I had it shortened with a variable but I removed it to see if it was causing a problem.

I checked the spelling, model location, made sure everything was right but it is still giving an error. Even when I use code completion it gives the same error. This has me really confused...

1 answer

Log in to vote
1
Answered by 8 years ago

Using "WaitForChild" will wait until something is there. If it isn't there, then it will not continue.

local collector=workspace.Tycoon:WaitForChild("Collector")--This might work.
print("It's there!") 
script.Parent.Touched:connect(function(hit)
    if hit.Name=="Copper" then
    print'Found "Copper"'--Don't worry, prints can be written out like that.
    collector.Cash.Value=collector.Cash.Value+2
    print'Done!' --Just to make sure the function is finished.
    end
end)
0
I am going to accept this answer just because it is here. A script was constantly changing the name of the collector model to show the ammount of money. AZDev 590 — 8y
0
This script would indeed, not work, due to using Cash's value in the 'collector' variable, which would 'remember' the original value, not the changed value. rexbit 707 — 8y
Ad

Answer this question