DataSave is not working

I am trying to make it so that ur backpack saves and when you rejoin, the items u had are still there (idk how to make an inventory lol)
This code works in Roblox Studio, but it doesnt work on Roblox.

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = 'leaderstats'
	local rolls = Instance.new("IntValue", folder)
	rolls.Name = 'Rolls'
	rolls.Value = 0
	
luckmultiplier = Instance.new("IntValue", folder)
	luckmultiplier.Name = 'Luck'
	luckmultiplier.Value = 0
	luckmultiplier.Changed:Connect(function(newvalue)
		luckmultiplier.Value = newvalue
		print(newvalue)
	end)
	
	local playerid = "Player_" .. plr.UserId
	local data = rollstore:GetAsync(playerid)
	if data then
		if data['Rolls'] then
			rolls.Value = data['Rolls']
		else
			rolls.Value = 0
		end
		
	else
		rolls.Value = 0
	end
	if data['ItemTable'] then
		print('ItemTable is not nil')
		for i,v in ipairs(data['ItemTable']) do
			local foods = game.ReplicatedStorage.food:GetChildren()
print('Checkinggfood for loop')
print(v)
			for i,food in pairs(foods) do
				print(food.Name)
				print('Entering food for loop')
				print(v.. '. Comparison: ' .. tostring(food.Name))
				if v == food.Name then
					print('creating tool')
				
				local tool = Instance.new('Tool', plr.Backpack)
				print(tool.Parent)
					local itemclone = food:Clone()
					tool.Name = food.Name
					itemclone.Name = "Handle"
					itemclone.Parent = tool
					itemclone.Anchored = false
					itemclone:FindFirstChild("take"):Destroy()
				end
			end
		end
		
	else
		print('its nil rn')
	end

	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local items = {}
	local backpack = plr.Backpack


for i, item in backpack:GetChildren() do
		table.insert(items, item.Name)
		print(item.Name)
end

for i, itemers in ipairs(items) do
	print(itemers)
end
	local datatobesaved = {
		Rolls = plr.leaderstats.Rolls.Value,
		ItemTable = items
		
	}
	
	local playerid = "Player_" .. plr.UserId

	local success, err = pcall(function()
		rollstore:SetAsync(playerid,datatobesaved)
	end)
	
	if success then
		print("data saved!")
	else
		print("data failed to save!")
	end
end)
1 Like

The code seems correct, are you sure you are publishing the place?

yes i am, i dont know whats wrong

Are you getting any errors? There may be a corrupted save

no i dont have any errors printed. once again it works on roblox studio but not on roblox.

Have you tried printing out the data to see if anything exists

It looks like you need to add a BindToClose function. I think the game might be closing before it has time to save the data.

local runService = game:GetService("RunService")
local players = game:GetService("Players")

--first, you need to convert your saving function to something like this:
local function save(player)
    --saving code goes here
end

game:BindToClose(function()
    if runService:IsStudio() then task.wait(3) return nil end
    for _, player in ipairs(players:GetPlayers()) do
        save(player)
    end
    task.wait(2)
end)

(In your loading: GetAsync also needs a pcall, it can fail as well)

do i put my player.Removing function in the save() function?

Do:
players.PlayerRemoving:Connect(save)

I am assuming the code in my player.Removing function goes in save()?
and how could i keep my “data” variable if i do a pcall() ?

You can call a function in a function and assign it to a variable. Make sure to have it global unless it will be lost after pcall function concludes

local data
local success, err = pcall(function() 
data = DataStore:GetAsync(playerid)
end) 

if success then
      -- what you want to do with data
else
      warn("Something went wrong during data download. Error:".. err)  
end

Also, you should make your PlayerId variable atomic. Actually you’re setting it up in two different functions which can be easily broke.

local function getPlayerId()
       return "Player_" .. plr.UserId
end

local playerid = getPlayerId() -- now it's set up once

Hey kingbob, nothing prints

–Player Removing
for i, item in pairs(plr.Backpack:GetChildren()) do
table.insert(items, item.Name)
print(item.Name) – prints the items
end

–Player Added
for i,v in ipairs(data[‘ItemTable’]) do
print(v… ’ is in itemtable’) – this is not printing
end

local datatobesaved = {
		Rolls = plr.leaderstats.Rolls.Value,
		ItemTable = items

	}
	
	print('saved data')

	local playerid = "Player_" .. plr.UserId
print(playerid)
	local success, err = pcall(function()
		rollstore:SetAsync(playerid,datatobesaved)
		print(rollstore)
		print('currently in pcall')
		
	end)

I quickly modified your script.
Here it is:

local players = game:GetService("Players")
local runService = game:GetService("RunService")

local function load(plr:Player)
	local folder = Instance.new("Folder", plr)
	folder.Name = 'leaderstats'
	local rolls = Instance.new("IntValue", folder)
	rolls.Name = 'Rolls'
	rolls.Value = 0

	local luckmultiplier = Instance.new("IntValue", folder)
	luckmultiplier.Name = 'Luck'
	luckmultiplier.Value = 0
	luckmultiplier.Changed:Connect(function(newvalue)
		luckmultiplier.Value = newvalue
		print(newvalue)
	end)

	local playerid = "Player_" .. plr.UserId
	local data
	local success, err = pcall(function()
		data = rollstore:GetAsync(playerid)
	end)
	if data then
		if data['Rolls'] then
			rolls.Value = data['Rolls']
		else
			rolls.Value = 0
		end

	else
		rolls.Value = 0
	end
	if data['ItemTable'] then
		print('ItemTable is not nil')
		for i,v in ipairs(data['ItemTable']) do
			local foods = game.ReplicatedStorage.food:GetChildren()
			print('Checkinggfood for loop')
			print(v)
			for i,food in pairs(foods) do
				print(food.Name)
				print('Entering food for loop')
				print(v.. '. Comparison: ' .. tostring(food.Name))
				if v == food.Name then
					print('creating tool')

					local tool = Instance.new('Tool', plr.Backpack)
					print(tool.Parent)
					local itemclone = food:Clone()
					tool.Name = food.Name
					itemclone.Name = "Handle"
					itemclone.Parent = tool
					itemclone.Anchored = false
					itemclone:FindFirstChild("take"):Destroy()
				end
			end
		end

	else
		print('its nil rn')
	end
end

local function save(plr:Player)
	local items = {}
	local backpack = plr.Backpack


	for i, item in backpack:GetChildren() do
		table.insert(items, item.Name)
		print(item.Name)
	end

	for i, itemers in ipairs(items) do
		print(itemers)
	end
	local datatobesaved = {
		Rolls = plr.leaderstats.Rolls.Value,
		ItemTable = items

	}

	local playerid = "Player_" .. plr.UserId

	local success, err = pcall(function()
		rollstore:UpdateAsync(playerid, function(old)
			for i, v in pairs(datatobesaved) do
				if i == nil or v == nil then
					datatobesaved = old
				end
			end
			
			--you can add individual checks to make sure each data element
			--is there and ready to be saved.
			
			return datatobesaved
		end)
	end)

	if success then
		print("data saved!")
	else
		print("data failed to save!")
	end
end

local function onGameClose()
	if runService:IsStudio() then task.wait(3) return nil end
	for _, player in ipairs(players:GetPlayers()) do
		save(player)
	end
	task.wait(3)
end

players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)
game:BindToClose(onGameClose)

Hey, 12345koip,
this code for some reason only works on roblox studio. It does not work on roblox tho :frowning:
(sorry for the extremly late response)

Could you test your script and tell me if it works on roblox to know if its a me problem or not

Is tgere anything in the output? Can you add a few print statements and let me know what prints and what doesn’t?

	'item is finished and should now be in player backpack'

However there are times where it is in my backpack, and sometimes nothing is in

“itemtable is not nil” also prints
“creating tool” prints
“should be saved rn” does not print

I checked on ROBLOX and all the things i said prints does also print in roblox, but i never have an item in my inventory

Edit 3: After further testing there is basically a 50% chance it is in my inventory. Everything i say prints does.

local players = game:GetService("Players")
local runService = game:GetService("RunService")

local function load(plr:Player)
	local folder = Instance.new("Folder", plr)
	folder.Name = 'leaderstats'
	local rolls = Instance.new("IntValue", folder)
	rolls.Name = 'Rolls'
	rolls.Value = 0

	luckmultiplier = Instance.new("IntValue", folder)
	luckmultiplier.Name = 'Luck'
	luckmultiplier.Value = 1
	luckmultiplier.Changed:Connect(function(newvalue)
		luckmultiplier.Value = newvalue
		print(newvalue)
	end)

	local playerid = "Player_" .. plr.UserId
	local data
	local success, err = pcall(function()
		data = rollstore:GetAsync(playerid)
	end)
	if data then
		if data['Rolls'] then
			rolls.Value = data['Rolls']
		else
			rolls.Value = 0
		end

	else
		rolls.Value = 0
	end
	if data['ItemTable'] then
		print('ItemTable is not nil')
		for i,v in ipairs(data['ItemTable']) do
			local foods = game.ReplicatedStorage.food:GetChildren()
			
			for i,food in pairs(foods) do
				print(food.Name)
				
				if v == food.Name then
					print('creating tool')

					local tool = Instance.new('Tool', plr.Backpack)
					
					local itemclone = food:Clone()
					tool.Name = food.Name
					itemclone.Name = "Handle"
					itemclone.Parent = tool
					itemclone.Anchored = false
		itemclone:FindFirstChild("take"):Destroy()
		print('item is finished and should now be in player backpack') 
					module.ItemUse(itemclone.LuckBooster.Value, tool, itemclone.Time.Value, plr, v)
				end
			end
		end

	else
		print('its nil rn')
	end
end

local function save(plr:Player)
	local items = {}
	local backpack = plr.Backpack


	for i, item in backpack:GetChildren() do
		table.insert(items, item.Name)
	end

	for i, itemers in ipairs(items) do
		print(itemers)
	end
	local datatobesaved = {
		Rolls = plr.leaderstats.Rolls.Value,
		ItemTable = items

	}
	
	print('data saved')

	local playerid = "Player_" .. plr.UserId

	local success, err = pcall(function()
		rollstore:UpdateAsync(playerid, function(old)
			for i, v in pairs(datatobesaved) do
				if i == nil or v == nil then
					datatobesaved = old
				end
			end

			--you can add individual checks to make sure each data element
			--is there and ready to be saved.

			return datatobesaved
		end)
	end)

	if success then
		print("data saved!")
	else
		print("data failed to save!")
	end
end

local function onGameClose()
	if runService:IsStudio() then task.wait(3) return nil end
	for _, player in ipairs(players:GetPlayers()) do
		save(player)
print('should be saved rn')
	end
	task.wait(3)
end

players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)
game:BindToClose(onGameClose)

image

btw the “doritos, chips4noobs, pringles” is what is supposed to be in the backpack (for i,v loop).