Making a button visible only if a leaderstats value is equal to 1 for example

  1. What do you want to achieve?

I’m trying to script a button so that its visibility state is only true when the leaderstats value is equal to 1 for example.

  1. What is the issue?

I’ve tried to figure it out myself, but my knowledge on scripting isn’t the best! pointing me in the right direction would be really useful - keep in mind I don’t want you to tell me the answer directly.

  1. What solutions have you tried so far?

There isn’t much in the way of this topic (that I have found) but I would be grateful if you could reply with links to other posts regarding this!

This is my current code in a local script under the button in question.

while wait () do
	if game.Players.LocalPlayer.leaderstats.Stage.Value == 1 then
	script.Parent.Visible = true else
	script.Parent.Visible = false
	end

Thanks! :slight_smile:

1 Like

Replace your code with this:

game.Players.LocalPlayer.leaderstats.Stage:GetPropertyChangedSignal('Value'):Connect(function(NewValue)
    if NewValue == 1 then
        script.Parent.Visible = true
    else
        script.Parent.Visible = false
    end
end)
1 Like

That can be changed to Stage.Changed:Connect

2 Likes

That would also detect the input for the name changed, and all that.

1 Like

Unfortunately, this code doesn’t seem to make the gui visible! Thank you for trying to help though.

1 Like

what script are you using? LocalScript or ServerScript?

1 Like
game.Players.LocalPlayer.leaderstats.Stage.Changed:Connect(function(value)
    if value == 1 then
        script.Parent.Visible = true
    else
        script.Parent.Visible = false
    end
end)
4 Likes

Actually it wouldn’t, .Changed is special for IntValues/BoolValues/StringValues/ObjectValues/NumberValues. It will only detect changes ln the .Value property

1 Like

Thank you for everyones attempts to help! I have found another way around this and it seems to be working.

1 Like

A short way of what @5uphi suggested -

game.Players.LocalPlayer.leaderstats.Stage.Changed:Connect(function(value)
	script.Parent.Visible = value == 1
end)
2 Likes

you don’t need the and/or part :wink:
this is all you need

script.Parent.Visible = value == 1
2 Likes

Oh my bad, thanks for correcting !

I used to use the or/and because it’s commonly used with such cases, but nice

1 Like
print(1 == 1) -- true
print(1 == 2) -- false
print("ABC" == "ABC") -- true
print("CDA" == "GDG") -- false
2 Likes

Yeah, these ones I kind of know,
but wouldnt we need to use or/and sometimes to shorten if statements ?
[Example, door, or something like that]

like a toggle?

doorOpen = not doorOpen

Like here -

local object = "blue"
local newCFrame = CFrame.new(object == "blue" and 215 or 100, object == "blue" and 424 or 91, object == "blue" and 111 or 125)

this example shows the best of what I mean
‘and’ and ‘or’ are used here to shorten the if statement

1 Like