Na początku należy włączyć API w ustawieniach gry: Game Settings / Security / Enable Studio Access to API Services
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("MyData")
local function AddLeaderstatsBoard(player)
local leaderBoard = Instance.new("Folder", player)
leaderBoard.Name = "leaderstats"
local coins = Instance.new("IntValue", leaderBoard)
coins.Name = "Coins"
end
local function IntializeData(player)
local data = nil
local success, msg = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if not success then
print("Data failure", msg)
end
if data then
player.leaderstats.Coins.Value = data.Coins
else
data = {Coins = 0}
print("You play for the first time")
end
dataStore:SetAsync(player.UserId, data)
end
local function SaveData(player)
local data = {}
data.Coins = player.leaderstats.Coins.Value
dataStore:SetAsync(player.UserId, data)
end
game.Players.PlayerAdded:Connect(function(player)
AddLeaderstatsBoard(player)
IntializeData(player)
player.leaderstats.Coins.Changed:Connect(function()
SaveData(player)
end)
end)
Zapisywanie statystyk leaderstats: Coins i Kills
local dataStoreService = game:GetService("DataStoreService")
local leaderStore = dataStoreService:GetDataStore("LeaderStore")
local function AddLeaderstatsBoard(player)
local leaderBoard = Instance.new("Folder", player)
leaderBoard.Name = "leaderstats"
local coins = Instance.new("IntValue", leaderBoard)
coins.Name = "Coins"
local kills = Instance.new("IntValue", leaderBoard)
kills.Name = "Kills"
end
local data
local function SaveLeaderStats(player, data)
leaderStore:SetAsync(player.UserId, data)
end
local function IntializeCoinData(player)
data = nil
local success, msg = pcall(function()
data = leaderStore:GetAsync(player.UserId)
end)
if not success then
print("Data failure", msg)
end
if not data then
data = {Coins = 0, Kills = 0}
print("First time")
SaveLeaderStats(player, data)
end
if data then
player.leaderstats.Coins.Value = data.Coins
player.leaderstats.Kills.Value = data.Kills
end
end
game.Players.PlayerAdded:Connect(function(player)
AddLeaderstatsBoard(player)
IntializeCoinData(player)
--SaveLeaderStats(player, false) -- uncomment and run once to reset saved data
player.leaderstats.Coins.Changed:Connect(function()
local d = {
Coins = player.leaderstats.Coins.Value,
Kills = player.leaderstats.Kills.Value
}
SaveLeaderStats(player, d)
end)
-- add an event to update and save Kills count
end)
Zapisywanie itemów po zakupie w sklepie
Stwórz BindableEvent w ServerStorage o nazwie SaveItemsBE
local prompt = script.Parent.ProximityPrompt
local shopRE = game.ReplicatedStorage.ShopRE
local shopItems = game.ReplicatedStorage.ShopItems
prompt.Triggered:Connect(function(player)
shopRE:FireClient(player)
end)
shopRE.OnServerEvent:Connect(function(player, itemName, price)
local item = shopItems:WaitForChild(itemName)
if price < 0 then
return
end
if item then
local itemClone = item:Clone()
itemClone.Parent = player.Backpack
player.leaderstats.Coins.Value -= price
-- saving item part
local saveItemsBE = game.ServerStorage.SaveItemsBE
saveItemsBE:Fire(player, itemName)
end
end)
Skrypt W SerwerScriptService ItemsManager
local dataStoreService = game:GetService("DataStoreService")
local store = dataStoreService:GetDataStore("ItemStore")
local items = game.ReplicatedStorage:WaitForChild("ShopItems")
local saveItemsBE = game.ServerStorage.SaveItemsBE
local data
local function SaveData(player, data)
store:SetAsync(player.UserId, data)
end
local function IntializeData(player)
data = nil
local success, msg = pcall(function()
data = store:GetAsync(player.UserId)
end)
if not success then
print("Data failure", msg)
end
if not data then
data = {}
print("First time")
SaveData(player, data)
end
if data then
for key, value in pairs(data) do
local item = items:FindFirstChild(data[key])
print(value)
local newItem = item:Clone()
newItem.Parent = player.Backpack
end
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
IntializeData(player)
saveItemsBE.Event:Connect(function(player, itemName)
print("save item "..itemName.." to "..player.Name)
table.insert(data, itemName)
SaveData(player, data)
end)
end)
--SaveData(player, false) -- uncomment and run once to reset saved data
end)