So I’m trying to make a datastore for my clicker game, and it won’t work, I don’t really know why, there’s no errors or anything, it just doesn’t save.
I haven’t tested if it works in game, but in studio it doesn’t work, so i’m just wondering what could be wrong with it if theres no errors.
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local database = DataStoreService:GetDataStore("data")
local sessiondata = {}
function PlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Clicks = Instance.new("NumberValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Rebirths = Instance.new("NumberValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = leaderstats
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(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 = {
["Clicks"] = 0,
["Rebiths"] = 0
}
end
sessiondata[player.UserId] = playerData
else
warn("Unable to get data for "..player.UserId)
player:Kick("Unable to load data, Try again")
end
Clicks.Value = sessiondata[player.UserId].Clicks
Rebirths.Value = sessiondata[player.UserId].Rebirths
Clicks.Changed:Connect(function()
sessiondata[player.UserId].Clicks = Clicks.Value
end)
Rebirths.Changed:Connect(function()
sessiondata[player.UserId].Rebirths = Rebirths.Value
end)
leaderstats.Parent = player
end
Players.PlayerAdded:Connect(PlayerAdded)
function PlayerLeaving(player)
if sessiondata[player.UserId] then
local success = nil
local errormsg = nil
local attempt = 1
repeat
success, errormsg = pcall(function()
database:SetAsync(player.UserId, sessiondata[player.UserId])
end)
attempt += 1
if not success then
warn(errormsg)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for "..player.Name)
else
warn("Data could not save for "..player.Name)
end
end
end
Players.PlayerRemoving:Connect(PlayerLeaving)
function ServerShutdown()
if RunService:IsStudio() then
return
end
print("Server Shutting down")
for i, player in ipairs(Players:GetPlayers()) do
task.spawn(function()
PlayerLeaving(player)
end)
end
end
game:BindToClose(ServerShutdown)
And yes, I have api services enabled