My button on my GUI is not turning visible like it should when this value hits 50 the script is a local script because it is in starter GUI. I'm not sure to why this is happening can someone help me?
local player = game.Players.LocalPlayer local Subscribers = player.leaderstats.Subscribers.Value while true do wait(2) if Subscribers >= 50 then player.PlayerGui.ComputerGUI.ComputerGui.CreateTier2Video.Visible = true end end
The problem here is the fact that you wrongly assume that the Subscribers
variable is a reference to the property of the Subscribers IntValue/NumberValue
.
To solve this problem, you would have the variable hold the IntValue/NumberValue
object itself, and make a direct access to the Value
property.
It is important that you know the difference between a reference
and a value
.
In Lua, for example, functions are passed by reference.
local f1, f2, f1ref f1 = function() for i = 1, 10 do print(i^2) end end f2 = function() for i = 1, 10 do print(i^2) end end f1ref = f1
f1ref
and f1
have the same exact function in memory, and f2
is a different function from f1
even if it has the same exact code, indentation, and line count (not that indentation would mean anything significant in Lua, but just to point out the similarities).
However things like booleans and numbers are passed simply via value.
local _string = "hello" local _string_value = _string _string = "world" print(_string_value) local bool1 = false local bool1_val = bool1 bool1 = true print(bool1) --> true print(bool1_val) --> false
Even though _string_value
was set to _string
, both variables have different memory and modifying one value does not change the other. No prize for guessing what happens with the boolean values.
local player = game:GetService("Players").LocalPlayer -- # recommended way of getting services local Subscribers = player.leaderstats.Subscribers Subscribers.Changed:Connect(function(subCount) -- # subCount is the new subscriber count. if subCount >= 50 then player.PlayerGui.ComputerGUI.ComputerGui.CreateTier2Video.Visible = true end end
Notice the Changed
event. You only need to check when the value changes. Not every two seconds.