So i just finished the gnome code’s video on how to make a datastore and it doesn’t work, I’m kinda clueless right now because i don’t even have any errors or anything like that. I have the datastore enabled in game settings, but still nothing.
local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")
local database = DatastoreService:GetDataStore("Datastore")
local sessionData = {}
function playerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Cash = Instance.new("NumberValue", leaderstats)
Cash.Name = "Cash"
local Gems = Instance.new("NumberValue", leaderstats)
Gems.Name = "Gems"
local success = nil
local playerData = nil
local attempt = 1
local UserId = player.UserId
repeat
success, playerData = pcall(function()
return database:GetAsync(UserId)
end)
attempt += 1
if not success then
print(playerData)
task.wait(3)
end
until success or attempt == 5
if success then
print("Connected to database")
if not playerData then
print("Assigning default data")
playerData = {
["Cash"] = 15;
["Gems"] = 0;
["Pets"] = {
"Cat"
}
}
end
sessionData[UserId] = playerData
else
warn("Unable to get data for "..UserId)
player:Kick("Unable to load your data. Try again later.")
end
Cash.Value = sessionData[UserId].Cash
Gems.Value = sessionData[UserId].Gems
Cash.Changed:Connect(function()
sessionData[UserId].Cash = Cash.Value
end)
Gems.Changed:Connect(function()
sessionData[UserId].Gems = Gems.Value
end)
leaderstats.Parent = player
end
Players.PlayerAdded:Connect(playerAdded)
----------------------------------------------------------------
function playerLeaving(player)
local UserId = player.UserId
if sessionData[UserId] then
local success = nil
local errorMsg = nil
local attempt = 1
repeat
success, errorMsg = pcall(function()
database:SetAsync(UserId, sessionData[UserId])
end)
attempt += 1
if not success then
print(errorMsg)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for "..player.Name)
else
warn("Unable to save for "..player.Name)
end
end
end
Players.PlayerRemoving:Connect(playerLeaving)
function serverShutdown(player)
for i, player in ipairs(Players:GetPlayers()) do
task.spawn(function()
playerLeaving(player)
end)
end
end
game:BindToClose(serverShutdown)