Problem with datastore that has no errors

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

2 Likes

Hey there.

Your code looks good, however whenever data saving it’s recommended practice to test it in game versus publishing studio data to the store. If you need help afterwards, I’ll try to reproduce the problem.

1 Like

I tried it out in game and it still didn’t work

1 Like

Inside the PlayerRemoving function, add a print and lmk if it prints

1 Like

printed out
image

Unsure of what OP did, but make sure you have Studio API Access Enabled as the code seems to be working.

In the picture above, I joined for the first time, set my data on the server, rejoined and retrieved my Clicks value.

I have it on, just for some reason it wont save for me

From where are you changing the Click.Value? Server or client?

Pretty sure client, I have script inside of StarterPlayerScripts

local player = game.Players.LocalPlayer

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
	local stat = player.leaderstats.Clicks
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		stat.Value += 1
	elseif input.UserInputType == Enum.UserInputType.Touch then
		stat.Value += 1
	end
end)

You can’t replicate stats on the Client to Server without Communication to the server.
Remotes will be your best friend on this aspect.

Fire the Remote to the Server upon clicking, on the server handle the value adding.

1 Like

Just adding that the original code didn’t work for me because Players was not defined but I did receive an error message telling me so. So once I added:

local Players = game:GetService("Players")

It worked fine.

That’s not OP’s problem. As his photo shows, Data saves for his persona. OP utilizes client for handling stats, just forgetting to use Remotes.

When I wrote my response only the original post and your first reply were there so I didn’t see any photo or anything until I sent my reply.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.