Skip to content

Instantly share code, notes, and snippets.

@RobertCodez
Created July 30, 2021 16:47
Show Gist options
  • Save RobertCodez/66fe16e5e1ea131e295c59c27e59e957 to your computer and use it in GitHub Desktop.
Save RobertCodez/66fe16e5e1ea131e295c59c27e59e957 to your computer and use it in GitHub Desktop.
Value Datastore | Saves the values such as coins in your game
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("SlimeStore")
local function saveData(player)
local SavedData = {
player.leaderstats.Slimes.Value;
}
local success, errorM = pcall(function()
dataStore:SetAsync(player.UserId, SavedData)
end)
if success then
else
warn(errorM)
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Slimes = Instance.new("IntValue", leaderstats)
Slimes.Name = "Slimes"
local data
local success, errorM = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success then
if Slimes.Value ~= nil then
Slimes.Value = data[1]
else
Slimes.Value = 0
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorM = pcall(function()
saveData(player)
end)
if success then
print("Saved")
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, errorM = pcall(function()
saveData(player)
end)
if success then
print("Saved")
end
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment