Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I set up SetASync?

So I've been working on a leaderboard script and I'm having a bit of an issue with the SetASync command. I just need a bit of help setting it up. Here is the rest of the code

local DataStoreService = game:GetService("DataStoreService")
local moneyStore = DataStoreService:GetDataStore("moneyStore")

local player = game:GetService("Players")


local function leaderboardSetup(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    
    local cash = Instance.new("IntValue")
    cash.Name = "Money"
    cash.Value = 0
    cash.Parent = leaderstats
    
    local UserID = player.UserId
    local money = moneyStore:GetASync(UserID)
    
    if money then
        cash.Value = money
    else
        cash.Value = 0
    end
end



game.Players.PlayerAdded:Connect(leaderboardSetup)``

1 answer

✓ Accepted answer

Hope this helps in future work ;)

local DataStoreService = game:GetService("DataStoreService")
local moneyStore = DataStoreService:GetDataStore("moneyStore")

game.Players.PlayerAdded:Connect(function)player)                      
    local leaderstats = Instance.new("Folder",player) -- to parent it to player just put a comma and the parent
    leaderstats.Name = "leaderstats"

    local cash = Instance.new("NumberValue",leaderstats) -- User NumberValue not IntValue it works better
    cash.Name = "Money"
    cash.Value = moneyStore:GetAsync(player.UserId) -- you set the cash.value to the data store
end)

-- To Save your Data you do 

game.Players.PlayerRemoving:Connect(function(player)
local Cash = player.leaderstats.Money
moneyStore:SetASync(player.UserId,Cash.Value)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.