i was trying to make a way to store the collectibles in my game but i dont know anything about scripting
i made this using a tutorial on data stores but it doesnt seem to work
local currencyName = "Collectibles"
local Players = game.Players
local DataStoreService = game:GetService("DataStoreService")
local CollectiblesStore = DataStoreService:GetDataStore("CollectiblesStorage")
local function loadData(player)
local ID = currencyName.."-"..player.UserId
local data = nil
local success, err = pcall(function()
data = CollectiblesStore:GetAsync(ID)
end)
if data ~= nil then
local CollectiblesValue = player.leaderstats:FindFirstChild(currencyName)
if CollectiblesValue then
CollectiblesValue.Value = data
print("Data successfully loaded!")
end
else
local CollectiblesValue = player.leaderstats:FindFirstChild(currencyName)
if CollectiblesValue then
CollectiblesValue.Value = 0
print("New Player was given 100 Collectibles for joining the Game!")
end
end
end
local function saveData(player)
local ID = currencyName.."-"..player.UserId
local success, err = pcall(function()
local CollectiblesValue = player.leaderstats:FindFirstChild(currencyName)
if CollectiblesValue then
CollectiblesStore:SetAsync(ID, CollectiblesValue.Value)
end
end)
if success then
print("Successfully saved Player Data for Collectibles")
else
print(err)
end
end
game:BindToClose(function()
for i, player in pairs(Players:GetPlayers()) do
if player then
player:Kick("Game is shutting down!")
end
end
wait(1)
end)
Players.PlayerAdded:Connect(loadData)
Players.PlayerRemoving:Connect(saveData)