Problem with datastore?

So i have this datastore script, but it doenst work. There is no error, but when I join I have 5 cash and 5 rebirths, what is the problem?

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:connect(function(plr)
	local stats = Instance.new('Folder', plr)
	stats.Name = 'leaderstats'
	
	local values = Instance.new('Folder', plr)
	values.Name = 'Values'
	
	local Cash = Instance.new('IntValue', stats)
	Cash.Name = 'Cash'
	Cash.Value = 0
	
	local Rebirth = Instance.new('IntValue', stats)
	Rebirth.Name = 'Rebirths'
	Rebirth.Value = 0
	
	local Multiplier = Instance.new("IntValue", values)
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 1
	
	local clickCash = Instance.new('IntValue', values)
	clickCash.Name = 'clickCash'
	clickCash.Value = 1
	
	local playerUserId = "Player_"..plr.UserId
	
	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(playerUserId)
	end)
	
	if success then
		Cash.Value = data.Cash
		Rebirth.Value = data.Rebirth
		Multiplier.Value = data.Multiplier
		clickCash.Value = data.clickCash
	end
end)


game.Players.PlayerRemoving:Connect(function(plr)
	local playerUserId = "Player_"..plr.UserId
	
	local data = {
		Cash = plr.leaderstats.Cash.Value;
		Rebirth = plr.leaderstats.Cash.Value;
		Multiplier = plr.Values.Multiplier.Value;
		clickCash = plr.Values.clickCash.Value
	}
	
	local success, errormessage pcall(function()
		DataStore:SetAsync(playerUserId, data)
	end)

	if success then
		print("Data saved!")
	else
		print("Error with saving data")
		warn(errormessage)
	end
end)

You put plr.leaderstats.Cash.Value instead of plr.leaderstats.Rebirths.Value

Yeah oops, but it still doenst work. My cash and rebirths are still both 5

Did you change your the number of rebirths you had?

Yes, it still doesn’t work, i tried changing both rebirths and cash values while in the server and not the client

make sure api services are on also try adding this to your code at the bottom

game:BindToClose(function()
	for i ,v in pairs(game.Players:GetPlayers()) do
		v:Kick()
	end
	wait(3)
end)

Well you may have trouble saving data because of this first line here. You didn’t put an = between errormessage and pcall. That may be preventing the game from saving data. I would try that.

1 Like

I rewrote the entire script, the data now loads, but does not save, i found out it has something to do with this

local success, errormessage = pcall(function()
		DataStore:SetAsync(playerUserId, data)
	end)

We talked it over in DMs and turns out it was not saving because in studio the code stopped running before the data saved. Solved it by giving it time to save with BindToClose

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:connect(function(plr)
	local stats = Instance.new('Folder', plr)
	stats.Name = 'leaderstats'
	
	local values = Instance.new('Folder', plr)
	values.Name = 'Values'
	
	local Cash = Instance.new('IntValue', stats)
	Cash.Name = 'Cash'
	Cash.Value = 0
	
	local Rebirth = Instance.new('IntValue', stats)
	Rebirth.Name = 'Rebirths'
	Rebirth.Value = 0
	
	local Multiplier = Instance.new("IntValue", values)
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 1
	
	local clickCash = Instance.new('IntValue', values)
	clickCash.Name = 'clickCash'
	clickCash.Value = 1
	
	local playerUserId = "Player_"..plr.UserId
	
	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(playerUserId)
	end)
	
	
	if success then
		Cash.Value = data.Cash
		Rebirth.Value = data.Rebirths
		Multiplier.Value = data.Multiplier
		clickCash.Value = data.clickCash
	end
	print("Data Loaded")
end)



function saveData(player)
	local UserId = player.UserId
	
	local data = {
		Cash = player.leaderstats.Cash.Value;
		Rebirths = player.leaderstats.Rebirths.Value;
		Multiplier = player.Values.Multiplier.Value;
		clickCash = player.Values.clickCash.Value
	}
	
	local success, errormessage = pcall(function()
		DataStore:SetAsync(UserId, data)
	end)
	
	if not success then
		warn(errormessage)
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)

game:BindToClose(function()
	if RunService:IsStudio() then
		return
	end
	local players = Players:GetPlayers()
	for _, player in pairs(players) do
		saveData()
	end
end)
1 Like