Datastore Not Working

This is my Clicking datastore for a upcoming game. It should work but when I play the game it doesn’t work at all.
This is the game link to prove its not working: Fantasy Clickers [Testing] - Roblox
Please could somebody help.

local DSS =game:GetService("DataStoreService")
local ClickDataStore = DSS:GetDataStore("ClickDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local Stat = Instance.new("Folder")
	Stat.Name = "Leaderstats"..player.Name
	Stat.Parent = game.Players
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = Stat

	local data
	local success, errormessage = pcall(function()
		data = ClickDataStore:GetAsync(player.userId.."Clicks")
	end)
	if success then
		Clicks.Value = data
		print("Player Data successfully loaded!")
		print("Last saved Click data:", data)
	else
		print("There was an error when loading data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	print("Most recent Click data save:", player.leaderstats.Clicks.Value)

	local success, errormessage =  pcall(function()
		ClickDataStore:SetAsync(player.UserId.."Clicks",game.Players["LeaderStats"..player.Name].Clicks.Value)
	end)
	if success then
		print("Player Data successfully saved!")
		game.Players["LeaderStats"..player.Name]:Destroy()
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)
1 Like

If a player joins for the first time, data will be nil, so checking if “success” is true won’t be enough. What you need to do is to give default data, if no previous data exists. You can simply do this by changing the line with “GetAsync” to the following:

data = ClickDataStore:GetAsync(player.userId.."Clicks") or 0 -- If no data, give starter data
1 Like

Ok, thanks. I have changed it. But when I leave the game this is what is show in the output. It seems it cant save but I don’t know why.

  15:18:44.111  Physics-in of unidentified 1_968803  -  Studio
  15:18:44.210  Player Data successfully loaded!  -  Server  -  ClickDataStore:18
  15:18:44.210  Last saved Click data: 0  -  Server  -  ClickDataStore:19
  15:18:49.805  New tag editor version coming online; unloading the old version  -  Studio
  15:18:49.944  Most recent Click data save: 0  -  Server  -  ClickDataStore:27
  15:18:49.945  There was an error when saving data  -  Server  -  ClickDataStore:36
  15:18:49.945  LeaderStatsAapocalypse_Gaming is not a valid member of Players "Players"  -  Server  -  ClickDataStore:37
  15:18:49.956  New tag editor version coming online; unloading the old version  -  Studio
  15:18:49.943  Disconnect from ::ffff:127.0.0.1|50572  -  Studio
1 Like

The saving system is not very well done, and you should anticipate that the player will be completely gone as soon as possible. Here is an optimized version:

game.Players.PlayerRemoving:Connect(function(player)
    local PlayerClicks = player.leaderstats.Clicks.Value
	print("Most recent Click data save:", PlayerClicks )

	local success, errormessage =  pcall(function()
		ClickDataStore:SetAsync(player.UserId.."Clicks", PlayerClicks)
	end)
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

You’re checking the Players container for a given player’s stats.
You need to change this to:

player["LeaderStats"..player.Name]:Destroy()

And then change this:

To this:

local Stat = Instance.new("Folder", player)
Stat.Name = "Leaderstats"..player.Name
1 Like

Hey dude you shouldn’t be making a bunch of topics about the same thing, if you got another problem, comment on the already existing post please :slight_smile:

1 Like

How do I make my image button add 1 each time it is pressed to the Click leaderstat?

script.Parent.MouseButton1Click:Connect(function()
	wait(0)
	game.Players.LocalPlayer.leaderstats.Clicks.Value = game.Players.LocalPlayer.leaderstats.Clicks.Value + 1
end)

You’re changing the value in a Localscript. Just take this script and put it in a ServerScript in the same location as the Localscript:

script.Parent.MouseButton1Click:Connect(function()
     local player = script:FindFirstAncestorOfClass("Player")
     player["LeaderStats"..player.Name].Clicks.Value += 1
end)

Unfortunately it doesn’t work. Also I cant put it in ServerScriptService because it then it wont work with the image button.

-- make a remoteevent in replicated storage called ClickRemote
local clickRemote = game:GetService("ReplicatedStorage"):WaitForChild("ClickRemote")
local debounce = false
-- server
clickRemote.OnServerEvent:Connect(function(player)
	if debounce == false then
		player.leaderstats.Clicks.value += 1
		debounce = true
	end
	if debounce == true then
		wait(2)
	end
end)
-- local script
script.Parent.MouseButton1Click:Connect(function()
	clickRemote:FireServer()
end)
   -- or 
  --Server Script inside button
local debounce = false
	
game:GetService("Players").PlayerAdded:Connect(function(player)
	script.Parent.MouseButton1Down:Connect(function()
		if debounce == false then
			player.leaderstats.Clicks.value += 1
			debounce = true
		end
		if debounce == true then
			wait(2)
		end
	end)		
end)
1 Like

I created the remote event.
Where do I put these?

local clickRemote = game:GetService("ReplicatedStorage"):WaitForChild("ClickRemote")
local debounce = false
-- server
clickRemote.OnServerEvent:Connect(function(player)
	if debounce == false then
		player.leaderstats.Clicks.value += 1
		debounce = true
	end
	if debounce == true then
		wait(2)
	end
end)
-- local script
script.Parent.MouseButton1Click:Connect(function()
	clickRemote:FireServer()
end)
   -- or 
  --Server Script inside button
local debounce = false

There’s absolutely no checking for existing players before PlayerAdded can be fired, most of these replies need to stop.
You need to declare a function that handles players, not just branching directly off the event connection.

function PlayerHandle(Player)
--do stuff here
end
for i,v in next, game.Players:GetPlayers() do
 PlayerHandle(v)
end
game.Players.PlayerAdded:Connect(PlayerHandle)
1 Like

It literally tells you one inside the script where you had the mousebutton1down event and the remoteevent in a serverscript in serverscriptservice

Is this all one script?

local clickRemote = game:GetService("ReplicatedStorage"):WaitForChild("ClickRemote")
local debounce = false
-- server
clickRemote.OnServerEvent:Connect(function(player)
	if debounce == false then
		player.leaderstats.Clicks.value += 1
		debounce = true
	end
	if debounce == true then
		wait(2)
	end
end)

And then

script.Parent.MouseButton1Click:Connect(function()
	clickRemote:FireServer()
end)

Yes thats the serverside then the one below it is clientside

1 Like

I am so confused. So the bottom one goes in localscript in the button?

script.Parent.MouseButton1Click:Connect(function()
	clickRemote:FireServer()
end)

And

local clickRemote = game:GetService("ReplicatedStorage"):WaitForChild("ClickRemote")
local debounce = false
-- server
clickRemote.OnServerEvent:Connect(function(player)
	if debounce == false then
		player.leaderstats.Clicks.value += 1
		debounce = true
	end
	if debounce == true then
		wait(2)
	end
end)

Goes into a normal script where?

I don’t get how this is confusing the one that has the event for the button goes inside a localscript inside of the button which increases the players clicks.

The one with the debounce goes inside of a regular script/serverscript in serverscriptservice.

It is not saved because the server shuts down and does not let PlayerRemoving save the data.
Use this at the end:

local Players = game:GetService("Players")
game:BindToClose(function ()
    for i, Player in pairs(Players:GetPlayers()) do
        Save(Player)
    end
end)

Please note that the server will shut down after 60 seconds regardless of whether the scripts terminated or not.

Whats up with the text formatting?

1 Like

also that doesn’t work because the path is created in the datastore. eg it creates a folder called LeaderstatsAapocalypse_Gaming im my case and then in that makes the Clicks value.

game.Players.PlayerAdded:Connect(function(player)
	local Stat = Instance.new("Folder", player)
	Stat.Name = "Leaderstats"..player.Name
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = Stat 

So you cant use

player.leaderstats.Clicks.value += 1