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

Script is changing both leaderstats to the same one when saving. How do I fix this?

Asked by 3 years ago

So I'm trying to make a script that makes two leaderstats, Coins, and Level, but the problem that keeps happening is when the coins is changed, lets say the coins value was changed to 100. Then once I rejoin and the datastore saves my progress, it changes Level to the same amount as the coins, so basically the coins are 100, and the level is 100. Heres my code:

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("MondStats") 

game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local currency = Instance.new("IntValue", folder)
    currency.Name = "Coins" 
    currency.Value = ds:GetAsync(plr.UserId) or 0
    currency.Changed:Connect(function()
        ds:SetAsync(plr.UserId, currency.Value)
    end)
    local currency2 = Instance.new("IntValue", folder)
    currency2.Name = "Level" 
    currency2.Value = ds:GetAsync(plr.UserId) or 0
    currency.Changed:Connect(function()
        ds:SetAsync(plr.UserId, currency.Value)
    end)
    currency2.Changed:Connect(function()
        ds:SetAsync(plr.UserId, currency2.Value)
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    ds:SetAsync(plr.UserId, plr.leaderstats.Coins.Value, plr.leaderstats.Level.Value) 
end)

1 answer

Log in to vote
0
Answered by 3 years ago

I just figured it out! I wasn't creating the Level's own datastore that it could save on. The datastore the level was using was both of the leaderstats' datastores, which made the Coins value overwrite the level.

Ad

Answer this question