Trying to change text based on leaderstat value

Hello,
I am trying to make the text on a TextButton change depending on the time someone has spent in the game. The time spent in the game is saved as a leaderstat. However, the script I have used is not working and I have no clue why. Could someone help me?

local players = game:GetService("Players")
local player = players.LocalPlayer
local Time = player:WaitForChild("leaderstats").Time.Value
local Redeem = script.Parent.UGCFrame.Redeem

if Time >= 17 then
	Redeem.Text = "REDEEM"
	Redeem.LocalScript.Enabled = true
else
	Redeem.Text = "LOCKED"
end
1 Like

is this client or serverside, and is the code that changes the value/makes the stat client or serverside

1 Like

This is client but the code that changes the value is server side

1 Like

Are you detecting when It updates?

Time.Changed:Connect(function()
if Time.Value >=17 then
--Stuff
end
end) 
2 Likes

its not working because theres no loop or update checking on the stat
missed that the first time sorry
Edit: devnotewastaken beat me to it

3 Likes

this is probably the most efficient method, it will only read value changes

local players = game:GetService("Players")
local player = players.LocalPlayer
local Time = player:WaitForChild("leaderstats").Time
local Redeem = script.Parent.UGCFrame.Redeem

function Update()
	if Time.Value >= 17 then
		Redeem.Text = "REDEEM"
		Redeem.LocalScript.Enabled = true
	else
		Redeem.Text = "LOCKED"
	end
end

Update()

Time:GetPropertyChangedSignal("Value"):Connect(Update)
2 Likes

Nope I wasn’t. Thanks for the help

2 Likes

you could probably shorten this to

local players = game:GetService("Players")
local player = players.LocalPlayer
local Time = player:WaitForChild("leaderstats").Time
local Redeem = script.Parent.UGCFrame.Redeem

Time:GetPropertyChangedSignal("Value"):Connect(Function()
	if Time.Value >= 17 then
		Redeem.Text = "REDEEM"
		Redeem.LocalScript.Enabled = true
	else
		Redeem.Text = "LOCKED"
	end
end)

I never understood why people do the way you did it

that would update it from the beginning, so my code works more as intended. this would only update after the value changed

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.