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

How would someone rewrite this script to make it function correctly?

Asked by
Kitorari 266 Moderation Voter
7 years ago
ValueTest = workspace.Partscan.Sugar.Value

SugarLabel = workspace.Partno.SurfaceGui.Frame.Sugar


workspace.Partscan.Sugar.Value:Changed(function(Scan) --CURRENT ERROR--

if ValueTest == 1 then

   SugarLabel.Text = "Sugar 1/1"

end
end)

08:43:13.454 - Workspace.Partno.Script:6: attempt to index field 'Value' (a number value)

Summary: When the VALUE of "Sugar" -- A NumberValue -- changes to 1, The TEXT of the SurfaceGui-Frame-Sugar --A TEXT Label-- changes to "1/1" then ENDS.

For some reason even though many people have answered this question, It continues to fail.

I'm hoping if I can clarify what this script is doing someone can actually help me, or rewrite the script so that it functions, and I can study that.

Don't make me have to copy what Workspace's children are. I rather not.

Please help me, This script is thinning my patience. and I'm trying really hard to make it work >:I

Thanks..

2 answers

Log in to vote
1
Answered by
Link150 1355 Badge of Merit Moderation Voter
7 years ago
Edited 7 years ago

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()
    -- Do something
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:

-- The local variable 'v' is assigned a value of '3' and is now of type 'number'.
local v = Sugar.Value

-- This line evaluates, or transforms to, "print(3.Changed)", which makes no sense.
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)
    -- Do something with the new and/or old value.
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. ;)

0
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. Kitorari 266 — 7y
Ad
Log in to vote
1
Answered by
DanzLua 2879 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago
ValueTest = workspace.Partscan.Sugar

SugarLabel = workspace.Partno.SurfaceGui.Frame.Sugar


ValueTest:Changed(function()

if ValueTest.Value == 1 then

   SugarLabel.Text = "Sugar 1/1"

end
end)


To check which value it is we do

ValueTest.Value

You can't put .Value in the variable.

0
Oh. Okay. Kitorari 266 — 7y
0
It didn't work but, thanks anyways... Kitorari 266 — 7y
0
@Kitorari It should of worked, ValueTest is a numbervalue and SurgarLabel is a TextLabel correct? DanzLua 2879 — 7y

Answer this question