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

Why is this button not turning visible after the value hits a certain number?

Asked by 5 years ago

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
0
Not sure how leaderstats work but try to add a ".Value" in Subscribers (line 6) (Subscribers.Value) HeyItzDanniee 252 — 5y
0
There’s no need for it to be in a loop, also cut down the referencing, targeting an instance by one line through multiple descendants isn’t a good idea Ziffixture 6913 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

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.

Reference vs Value

It is important that you know the difference between a reference and a value.

Reference

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).

Value

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.

Final product

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.


Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions, then feel free to leave them down in the comments.
1
gg GoldAngelInDisguise 297 — 5y
Ad

Answer this question