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

DataStore and Value problem?

Asked by 4 years ago

[ game.StarterGui.ScreenGui ... TextButton ]
Hello Guys! I have a TextButton that's called "Upgrade", if I'll click this button the Money value (the leaderstats child) will decrease by the price over this TextButton

Illustration 1(one)
[ PRICE: 10$ ] --Price over the TextButton
[ UPGRADE ] --TextButton
[ MONEY: 631$ ] --Money(child of the leaderstats)

When I'll click this TextButton the money will decrease by the price over this TextButton

Illustration 2
[ PRICE: 15$ ] --Price over the TextButton
[ UPGRADE ] --TextButton
[ MONEY: 621$ ] --Money(child of the leaderstats)

BUT! When i'll leave the game the Money value from the Illustration 2 will not save... the Money value from the Illustration 1(one) will save, and now there's a big question... is this TextButton not changing the value of Money? or the DataStore isn't saving the changed value?

[ game.ServerScriptService.leaderstats ]
There's the code of the DataStore(leaderstats) system

local DS = game:GetService("DataStoreService"):GetDataStore("leaderstats")

game.Players.PlayerAdded:Connect(function (plr)
    local stats = Instance.new("Folder",plr)
    stats.Name = "leaderstats"

    local money = Instance.new("IntValue",stats)
    money.Name = "Money"

    local key = "stats-".. plr.UserId
    if DS:GetAsync(key) then
        money.Value = DS:GetAsync(key)
    else
        DS:SetAsync(key,0)
        money.Value = DS:GetAsync(key)
    end
end)

game.Players.PlayerRemoving:Connect(function (plr)
    local key = "stats-".. plr.UserId
    DS:SetAsync(key,plr.leaderstats.Money.Value)
end)

As you guys can see, the code should work perfectly why it's not working? It's the DataStore system not working or the TextButton? Please help me! That's (i think) the biggest problem in my "game"

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Your datastore works but the only problem I see here is the textlabel update. The value "Money" saved in the datastore but the value of the textlabel did not update.

To make the saved value apply in the text label is to use a method called GetPropertyChangedSignal(). For more information for this method: Click

Here is an example on how to update a textlabel.

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local leaderboard = player:WaitForChild("leaderstats")
local coins = leaderboard.Money
local txtLabel = script.Parent

txtLabel.Text = "Coins: "..coins.Value

coins:GetPropertyChangedSignal("Value"):Connect(function() 
    txtLabel.Text = "Coins: "..coins.Value
end)

Hope this helped!

Ad

Answer this question