There are several logical issues with your code. I'll try to address them one by one.
First of all, Changed is an Event Object, not a function. Events are not meant to be called, like you would a function. It is Roblox that "fires" them whenever something happens. Instead, you connect a function to them using their :connect() method:
object.Changed:connect(function()
end)
A function connected to an event is called back whenever the event it is connected to is fired by Roblox, hence their name: "callback function".
In other words, you could say a callback function is "subscribed" to an event and is notified by Roblox whenever that event occurs.
Next, notice how in the previous example I used a period between objectand Changed; and a colon between Changed and connect. This is because Changed is a property but connect is a method. Object properties are accessed with a single period whereas methods (functions associated to objects) are called using the colon notation.
Finally, your most important problem; the reason why the script errors in the first place.
'Sugar' is an Object of class NumberValue. Objects are a special type of value introduced by Roblox. They do not exist in standard Lua. Objects are composed of properties and methods. To know which properties and methods are available, you will have to look through the Class Reference section of the Roblox Wiki. Optionally, there is also the "Object Browser" view of the Studio, although it might be outdated.
As mentioned before, Objects do not exist in standard Lua. The only native Lua types are:
number;
string (character sequence);
boolean (true/false value);
nil (usually represent lack of useful value);
table (lists of values, each associated to a "key", sometimes named "field");
function;
and finally, coroutine.
Now, if we take a look at the Class Reference entry for the NumberValue class, we can see the 'Value' property is of type number. In your code, you are trying to access a field (or property) Changed through the period '.' operator from that number, like if it was a table or Object. This doesn't make sense to Lua: numbers can't have fields nor properties.
For example, lets say Sugar.Value is 3. Think of it this way:
local v = Sugar.Value
print(v.Changed)
Numbers themselves can't have properties, but Sugar is a NumberValue Object. If you look at the NumberValue page of Class Reference, you will see NumberValues indeed possess a Changed event property. If you click on it, you will be redirected to a page with a brief description as well as an usage example.
Which, if we follow, gives us:
game.Workspace.Sugar.Changed:connect(function(newValue)
end)
That should be it, if you need any more help, feel free to ask.
Hope it helped; If it did please mark my answer as accepted and optionally upvote, it helps us both. ;)
1 comment
Thank you, though I applied this to the script, it didn't work, I'm guessing I need a transfer script and need to rewrite this on.