I don't know what's wrong in this datasave script

local startamount = 0

local DataStore = game:GetService(“DataStoreService”)

local ds = DataStore:GetDataStore(“Clickssave”)

local ds1 = DataStore:GetDataStore(“Gemssave”)

local ds2 = DataStore:GetDataStore(“Rebirthsssave”)

game.Players.PlayerAdded:connect(function(player)

local leaderstats = Instance.new(“Folder”,player)

leaderstats.Name = “leaderstats”

local Clicks = Instance.new(“IntValue”,leaderstats)

Clicks.Name = “Clicks”

Clicks.Value = ds:GetAsync(player.UserId) or startamount

ds:SetAsync(player.UserId, Clicks.Value)

Clicks.Changed:connect(function()

ds:SetAsync(player.UserId, Clicks.Value)

end)

local Rebirths = Instance.new(“IntValue”,leaderstats)

Rebirths.Name = “Rebirths”

Rebirths.Value = ds2:GetAsync(player.UserId) or startamount

ds2:SetAsync(player.UserId, Rebirths.Value)

Rebirths.Changed:connect(function()

ds:SetAsync(player.UserId, Rebirths.Value)

end)

local Gems = Instance.new(“IntValue”,leaderstats)

Gems.Name = “Gems”

Gems.Value = ds1:GetAsync(player.UserId) or startamount

ds1:SetAsync(player.UserId, Gems.Value)

Gems.Changed:connect(function()

ds:SetAsync(player.UserId, Gems.Value)

end)

end)

game.Players.PlayerRemoving:connect(function(player)

ds:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Clicks”).Value)

ds1:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Gems”).Value)

ds2:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Rebirths”).Value)

end)

1 Like

Can you show the output of this script, or elaborate further what happened?

there is no error only of other scipts

Then maybe you could elaborate what exactly happened?

don’t relly know what’s elaborate is

Like explaining further about it.

i make the script but i tested in in game but i click then after i leave and rejoin the click goes to 0

Let me see your code.

local startamount = 0

local DataStore = game:GetService(“DataStoreService”)

local ds = DataStore:GetDataStore(“Clickssave”)

local ds1 = DataStore:GetDataStore(“Gemssave”)

local ds2 = DataStore:GetDataStore(“Rebirthsssave”)

game.Players.PlayerAdded:connect(function(player)

local leaderstats = Instance.new(“Folder”,player)

leaderstats.Name = “leaderstats”

local Clicks = Instance.new(“IntValue”,leaderstats)

Clicks.Name = “Clicks”

Clicks.Value = ds:GetAsync(player.UserId) or startamount

ds:SetAsync(player.UserId, Clicks.Value)

Clicks.Changed:connect(function()

ds:SetAsync(player.UserId, Clicks.Value)

end)

local Rebirths = Instance.new(“IntValue”,leaderstats)

Rebirths.Name = “Rebirths”

Rebirths.Value = ds2:GetAsync(player.UserId) or startamount

ds2:SetAsync(player.UserId, Rebirths.Value)

Rebirths.Changed:connect(function()

ds:SetAsync(player.UserId, Rebirths.Value)

end)

local Gems = Instance.new(“IntValue”,leaderstats)

Gems.Name = “Gems”

Gems.Value = ds1:GetAsync(player.UserId) or startamount

ds1:SetAsync(player.UserId, Gems.Value)

Gems.Changed:connect(function()

ds:SetAsync(player.UserId, Gems.Value)

end)

end)

game.Players.PlayerRemoving:connect(function(player)

ds:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Clicks”).Value)

ds1:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Gems”).Value)

ds2:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Rebirths”).Value)

end)

Are you using the second value of Instance.new()? That got deprecated a while ago. Instead, just parent the value using .Parent
Also, I have a working code that maybe you could borrow. Maybe try it? (and replace the things you don’t need with the things you do need)

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player)  
	local leaderstats = Instance.new("Folder")  
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local corn = Instance.new("IntValue")
	corn.Name = "corn"
	corn.Value = 0
	corn.Parent = leaderstats

	local timeupg = Instance.new("IntValue")
	timeupg.Name = "timeupg"
	timeupg.Value = 0
	timeupg.Parent = leaderstats

	local valupg = Instance.new("IntValue")
	valupg.Name = "valupg"
	valupg.Value = 0
	valupg.Parent = leaderstats

	local capupg = Instance.new("IntValue")
	capupg.Name = "capupg"
	capupg.Value = 0
	capupg.Parent = leaderstats
	
	local playerUserId = "Player_" .. player.UserId  
	local data = playerData:GetAsync(playerUserId)  
	if data then
		corn.Value = data['corn']
		timeupg.Value = data['timeupg']
		valupg.Value = data['valupg']
		capupg.Value = data['capupg']
	else
		corn.Value = 0
		timeupg.Value = 0
		valupg.Value = 0
		capupg.Value = 0
	end
end

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onPlayerExit(player)  
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, player_stats) 
	end)

	if not success then
		warn("Could not save data!")
	end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

It seems like from comparing my code to your code, you never got the playerData, and instead got the values wrong. Maybe?

  1. You can use 1 GetDataStore and save every value you have in there with use of a table.
  2. Use a for i, v in pairs loop to create your stats, otherwise it will be a huge mess if you have 50+ stats for example.
  3. You are using multiple SetAsyncs. Its not needed, Use 1 UpdateAsync on plr left and BindToClose event, and save the stats inside a table.
1 Like

The code could be taking startamount before :GetAsync() finish. You should fix it so that the code checks for :GetAsync() before it sets to 0. For example with clicks:

local Clicks = Instance.new(“IntValue”)
Clicks.Parent=leaderstats
Clicks.Name = “Clicks”
Clicks.Value = ds:GetAsync(player.UserId)
if not Clicks.Value then
    Clicks.Value=startamount
end
ds:SetAsync(player.UserId, Clicks.Value)

I also agree with @.BobbieTrooper’s reply on optimizing your code.

For example. Use a module script to put your stats in you wanna use

local module = {
    
    Stats = {
        "Cash",
        "Multi",
		"Rebirth",
		"UltraRebirth",
		"Prestige",
		"UltraPrestige"
    }   
}

return module

Then you add this to your normal script.

local StatTable = require(game.ReplicatedStorage.StatTable)

game.Players.PlayerAdded:Connect(function(plr)
	local Stats = Instance.new("Folder")
	Stats.Name = "Stats"
	Stats.Parent = plr

  for i , v in pairs(StatTable.Stats) do
		  local stat = Instance.new("NumberValue")
		  stat.Name = v
		  stat.Parent = Stats
      end
end)

This doesn’t give you a solution to your issue, but it for sure helps to optimize your code and prevent 200 lines of code.

By the way, it’s called IntValue

There shouldn’t be an error with strings like that, what does the error message says?

i retyped is and there was no error any more

stiil doesn’t work here is the code:

local startamount = 0

local DataStore = game:GetService(“DataStoreService”)
local ds = DataStore:GetDataStore(“Clickssave”)
local ds1 = DataStore:GetDataStore(“Gemssave”)
local ds2 = DataStore:GetDataStore(“Rebirthssave”)

game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new(“Folder”,player)
leaderstats.Name = “leaderstats”

local Clicks = Instance.new("IntValue")
Clicks.Parent=leaderstats
Clicks.Name = "Clicks"
Clicks.Value = ds:GetAsync(player.UserId)
if not Clicks.Value then
	Clicks.Value=startamount
end
ds:SetAsync(player.UserId, Clicks.Value)

local Rebirths = Instance.new("IntValue")
Rebirths.Parent=leaderstats
Rebirths.Name = "Rebirths"
Rebirths.Value = ds:GetAsync(player.UserId)
if not Rebirths.Value then
	Rebirths.Value=startamount
end
ds2:SetAsync(player.UserId, Rebirths.Value)

local Gems = Instance.new("IntValue")
Gems.Parent=leaderstats
Gems.Name = "Gems"
Gems.Value = ds:GetAsync(player.UserId)
if not Gems.Value then
	Gems.Value=startamount
end
ds1:SetAsync(player.UserId, Gems.Value)

end)

game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Clicks”).Value)
ds1:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Gems”).Value)
ds2:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Rebirths”).Value)
end)

image

  1. You don’t have to use SetAsync so many times, And there is not GetAsync to get the data

which one should i get rid of and which ones should stay

Do this in player removing first

game.Players.PlayerRemoving:connect(function(player)
print(player.UserId)
ds:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Clicks”).Value)
ds1:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Gems”).Value)
ds2:SetAsync(player.UserId, player.leaderstats:WaitForChild(“Rebirths”).Value)
end)

and tell what id does it print

is that all what i should delete