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

How to add a second leaderstat??

Asked by 2 years ago

Hi, I am having issues with my leaderstats. I have a datastore for money. It all works perfectly. So, I decided to duplicate the script and change money to gems and renamed the datastore to GEMSDataStore. Issue is the leaderstats only display one of the currency and I want it to display both.

Code:

local DSS = game:GetService("DataStoreService")

local DataStore = DSS:GetDataStore("GEMSDataStore")





game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("yum")

    leaderstats.Name = "leaderstats"

    leaderstats.Parent = player



    local Gem = Instance.new("IntValue")

    Gem.Name = "Gem"

    Gem.Parent = leaderstats



    local data

    local success, errorMessage = pcall(function()

        data = DataStore:GetAsync(player.UserId)

    end)

    if success and data ~= nil then

        print("Data successfully loaded!")

        Gem.Value = data.Gem

    else

        warn(errorMessage)

    end

end)



game.Players.PlayerRemoving:Connect(function(player)

    local leaderstats = player.leaderstats

    local data = {

        Gem = leaderstats.Gem.Value; }

    local success, errorMessage = pcall(function()

        DataStore:SetAsync(player.UserId,data)

    end)

    if success then

        print("Data successfully saved!")

    else

        warn(errorMessage)

    end

end)
0
In the instance when I was trying to fix this I put yum in the brackets to see if that will work that's why its there on line 11 theking66hayday 841 — 2y

1 answer

Log in to vote
1
Answered by 2 years ago

I tested and it worked, put a Normal Script in the ServerScriptService

local DSS = game:GetService("DataStoreService")
local CashData = DSS:GetDataStore("CashData")
local GemsData = DSS:GetDataStore("GemsData")

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

    local cash = Instance.new("IntValue", leaderstats)
    cash.Name = "Cash"

    local gems = Instance.new("IntValue", leaderstats)
    gems.Name = "Gems"

    local playerId = "Player_"..player.UserId

    local cashdata
    local gemsdata
    local success, errormessage = pcall(function()
        cashdata = CashData:GetAsync(playerId)
        gemsdata = GemsData:GetAsync(playerId)
    end)

    if success then
        cash.Value = cashdata
        gems.Value = gemsdata
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerId = "Player_"..player.UserId

    local cashdata = player.leaderstats.Cash.Value
    local gemsdata = player.leaderstats.Gems.Value

    local success, errormessage = pcall(function()
        CashData:SetAsync(playerId, cashdata)
        GemsData:SetAsync(playerId, gemsdata)
    end)

    if success then
        print("Saved")
    else
        print("There was an error")
        warn(errormessage)
    end
end)
0
Thank u This helps me a lot and I also see now how to add 2 datastores in leaderstats using 1 script. theking66hayday 841 — 2y
0
You're welcome Pedrougubtz 90 — 2y
Ad

Answer this question