Hello. Recently, I’ve made a battle pass/unlockable rewards ui for my game, and, it works but at the same time it doesn’t.
The way the UI works is there is a button which is not visible to the player, which then becomes visible when their pass level is X amount, X of course being the number. There is a button which acts like a level up button, which then changes their amount from let’s say 1 to 2, when it’s clicked.
This works and the UI becomes visible when the amount is X amount, but, when you leave and rejoin the game, it is not visible until you buy another level.
No the pass level (which is the value it’s linked to) saves, but the UI stays invisible to the player until they gain another level then it becomes visible.
You just have to make the UI visible when the player joins and the player’s data is loaded.
You can easily do this when you send the data to the player from a remote event.
A localscript will receive that event and you just script the UI to appear visible under certain conditions that you add.
local Button = script.Parent
local Player = game.Players.LocalPlayer
local leaderstats = Player.leaderstats
local PassLevel = leaderstats.PassLevel
game.Players.PlayerAdded:Connect(function()
if PassLevel.Value >= 1 then
Button.Visible = true
end
end)
PassLevel.Changed:Connect(function()
if PassLevel.Value >= 1 then
Button.Visible = true
end
end)
It was originally just:
local Button = script.Parent
local Player = game.Players.LocalPlayer
local leaderstats = Player.leaderstats
local PassLevel = leaderstats.PassLevel
PassLevel.Changed:Connect(function()
if PassLevel.Value >= 1 then
Button.Visible = true
end
end)
But with the added bit of code, it still does not work. Could you try help me with this?
So your level value will not trigger the “Changed” event until it is changed after the player spawns, that is why it doesn’t seem to show. To fix this, you can call a function after the script waits for the player character to load.
local Button = script.Parent
local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local PassLevel = leaderstats:WaitForChild("PassLevel")
local Character = Player.Character or Player:WaitForChild("Character")
-- I added the WaitForChild() functions because we want to make sure it finds our
-- variables before continuing the script.
function update()
if PassLevel.Value >= 1 then
Button.Visible = true
else
Button.Visible = false
end
end
update()
Typically, you would call the update() function when the player’s data is sent to the player via remote event, because it would properly listen for the values to change rather than wait for the character to load.
Wouldn’t the value technically change while it is being loaded from the DataStore? Although I’m assuming this occurs before the callback is connected to the “.Changed” event in the UI script.
local Button = script.Parent
local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local PassLevel = leaderstats:WaitForChild("PassLevel")
local Character = Player.Character or Player.CharacterAdded:Wait()
local isVisible = false
repeat
task.wait()
if PassLevel.Value >= 1 then
Button.Visible = true
isVisible = true
else
Button.Visible = false
end
until isVisible
Also just so you’re aware "Player:WaitForChild(“Character”) would be invalid as the character model isn’t a child of the player instance, it is instead a property of the child instance, hence “Player.CharacterAdded:Wait()” would be valid.
With the above repeat until loop, every frame the value is checked and if the condition is met the button is made visible and the loop terminates.
For your question about the value changing while being loaded from the DataStore,
Using “Changed” is definitely important to keep, but on initial start of the game or even joining an active server, the connection would be inconsistent because of 2 main reasons: rendering speed (CPU) and network connection (ping). What I mean is, the value would change before the connection is made, so to ensure that the player receives the data no matter what, I would recommend having both connections.