Save leaderstats to DataStore

0

ServerScriptService/PlayerSetup
local DataStoreService = game:GetService("DataStoreService")
local playerGold = DataStoreService:GetDataStore("playerGold")

local function onPlayerJoin(player)
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local gold = Instance.new("IntValue", leaderstats)
    gold.Name = "Gold"

    local success, data = pcall(function()
        return playerGold:GetAsync("Player_"..player.UserId)
    end)
    if success then
        gold.Value = data
    end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)

local function onPlayerLeave(player)    
    local success, err = pcall(function()
        playerGold:SetAsync("Player_"..player.UserId, player.leaderstats.Gold.Value)
    end)
end
game.Players.PlayerRemoving:Connect(onPlayerLeave)
  • Add a part
  • Rename it to "Gold"
    • add a ClickDetector
    • add the following Script
Workspace/Gold/Script
local Part = script.Parent

local function onClick(player)
    local playerGold = player.leaderstats.Gold
    playerGold.Value = playerGold.Value + 1
end

Part.ClickDetector.MouseClick:Connect(onClick)

  • 0 posts
  • 0 subtopics
  • 9 days ago by vince