Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this script erroring? [closed]

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

✓ Accepted answer

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)

2 comments

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.