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

How do I add another IntValue to the leaderboard?

Asked by 4 years ago

I watched a video on how to add currency, and now i want to add 2 currencies. I don't exactly know how.. I am very new to this if i could get some help that would be great

local currencyName = "Zimo"
local DataStore = game:GetService("DataStoreService"):GetDataStore("Currency")

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

    local currency = Instance.new("IntValue")
    currency.Name = currencyName
    currency.Parent = folder

    local ID = currencyName.."-"..player.UserId
    local savedData = nil

    pcall(function()
        savedData = DataStore:GetAsync(ID)
    end)    

    if savedData ~= nil then
        currency.Value = savedData
        print("Data Loaded")
    else
        --New player--
        currency.Value = 750
        print("New player to game")
    end 
end)

game.Players.PlayerRemoving:Connect(function(player)
    local ID = currencyName.."-"..player.UserId
    DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
    -- When game is ready to shutdown
    for i, player in pairs(game.Players:GetPlayers()) do
        if player then
            player:Kick("Server shuting down")
        end
    end

    wait(5)
end)

Also if there is a better way to do this im open for it.

1 answer

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

Just use the same code you used before

local currency = Instance.new("IntValue") --- change currency to the other currency
    currency.Name = currencyName -- Change The Name
    currency.Parent = folder

If You Wanted Cash You'd Do

local Cash = Instance.new("IntValue")
    Cash.Name = Cash
    Cash.Parent = folder

The Datastore I use is

game.Players.PlayerRemoving:Connect(function(player)
    local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."leaderstats")
    local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
    for i = 1, #statstorage do
        datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
        print("Saved Data Number ("..i..")")
    end
    print("Stats Successfully Saved!")
end)

game.Players.PlayerAdded:Connect(function(player)
    local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."leaderstats")
    player:WaitForChild("leaderstats")
    wait(1)
    local stats = player:FindFirstChild("leaderstats"):GetChildren()
    for i = 1, #stats do
        stats[i].Value = datastore:GetAsync(stats[i].Name)
        print("stats number ("..i..") has been found")
    end
end)

Works Fairly Well

0
Would I make a new script? cause if i put another IntValue it breaks the data save for the other currency surferJG4 2 — 4y
0
Used have to make another Datastore because the current Datastore you're using only saves one currency Jomeliter 55 — 4y
0
Does it work for you? If so can you accept the answer? Jomeliter 55 — 4y
0
Sadly no I tried adding another script but it doesnt load half of the time idk why I could be doing something wrong.. surferJG4 2 — 4y
0
Did you put the Datastore in a serverscript? Jomeliter 55 — 4y
Ad

Answer this question