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

How would I check if a number value got updated?

Asked by 5 years ago
local Number = script.Parent.Number
local Player = game.Players.LocalPlayer:GetFullName()
local Max = script.Parent.Max.Value
local PlayerCrops = game.Workspace.CropsInBackpack.Value

Number.Text = (PlayerCrops.. " / ".. Max)

So, I want to check if a number value got updated. How would I do that? For example, If say a number value of 1 got updated to like a value of 3. How would I check for that? So I can update my Gui everytime the value gets changed.

2 answers

Log in to vote
0
Answered by 5 years ago

Hi there! What you could do is store an IntValue in a script, and then write something like this in another script, if I am correct:

script.Number.Value.Changed:Connect(function()
    --stuff here :D
end)

If this doesn't work, let me know!

0
Correction: "script" refers to the script with the IntValue, not the script that checks if the number value is updated. SnazzySpider67 53 — 5y
0
(IntValue.Value.Changed) will error. You have to do (IntValue.Changed) zyrun 168 — 5y
0
Thanks to your answer I found out about the Changed event, thank you so much! HarmonicPower 14 — 5y
0
Ah, thanks! Normally I would've caught that! SnazzySpider67 53 — 5y
Ad
Log in to vote
0
Answered by
zyrun 168
5 years ago

For if your number is actually a NumberValue object, not a variable:

local Number = script.Parent.Number -- The gui that will change
local NumberValueThatWillChange = game.Workspace.CropsInBackpack -- Hopfully this is the number that changes

NumberValueThatWillChange.Changed:Connect(function(Property)
    if Property = "Value" then -- Make sure that the value is changing, not the name or something
    Number.Text = tostring(NumberValueThatWillChange) -- tostring makes sure of no errors in that area
end)

For if your number is a variable:

local Number = script.Parent.Number -- The gui that will change
local NumberValueThatWillChange = 69 -- 69, Nice
local PastNumberValue = NumberValueThatWillChange -- To check for updates

while true do
    if PastNumberValue ~= NumberValueThatWillChange then
        PastNumberValue = NumberValueThatWillChange
        Number.Text = tostring(NumberValueThatWillChange) -- tostring makes sure of no errors in that area      
    end
    wait(.1)
end
0
Ah. I was unaware of that.I did mean to put the second '=' on line 5. Thank you for the correction though. The while loop was for the local variable, not an IntValue. zyrun 168 — 5y

Answer this question