Datastore works in studios but not in game

I am learning about datastore, and for that I made a very basic and simple tool saving system, which saves the data by pressing a gui button, and well this system does not work in the game but it does in the studio, I think I have found the reason why but I don’t know how to solve it.

In the save button script I put some print to know if the code works well or not, basically a print that prints the table in which the names of the tools that are in the backpack are saved, but in the game instead of print as it should, it prints something like “table: 0x00119290”

I will show you the script and 2 images, one in the studio and one in the game, showing the console and how that strange code is printed in the game:

button = script.Parent
plr =  button.Parent.Parent.Parent
datastore = game:GetService("DataStoreService")
tools = datastore:GetDataStore("tools")

button.MouseButton1Click:Connect(function()
	
	local save = {}
	for i,v in pairs(plr.Backpack:GetChildren()) do

		if v:IsA("Tool") then

			save[v.Name] = v:GetAttribute("ID")
		end
	end
	print(save)

	local suc,Error = pcall(function()

		tools:SetAsync(plr.UserId,save)
	end)

	if suc then
		print("ok")
	else
		print(Error)
	end
end)

in Studios:

in game:

The “table: (numbers here)” is normal. In Studio, it will just automatically expand it into the actual table. If you want to print() the table, you’d need to iterate through and print() each element individually. As for saving, can you send the script used to load the things as well?

Okay, I’ll send it, although I must clarify something curious, the code works, but in a strange way, if for example I am playing in the game (not in the studio) and I have a cheezburger and a sword in my backpack, and I press the save button, when I enter the game but in the studio, the data does load correctly and the 2 items appear in the backpack

in theory datastore shouldnt work through studio and server, ( i think )

datastore = game:GetService("DataStoreService")
tools = datastore:GetDataStore("tools")

game.Players.PlayerAdded:Connect(function(plr)
	
	local Data = tools:GetAsync(plr.UserId)
	
	if Data ~= nil then
		
		print(Data)
		
		for i,v in pairs(Data) do
			
			if game.ReplicatedStorage.Items:FindFirstChild(i):GetAttribute("ID") == Data[i] then
				
				game.ReplicatedStorage.Items:FindFirstChild(i):Clone().Parent = plr.Backpack
			end
		end
	else
		print("nil")
	end
end)

I forgot to mention, both the saving and load scripts are not local scripts.

I’m not exactly sure whats happening with your table, but it prints the table instance, the name of it, if anything try makin a script that checks what’s inside of the table, and that’ll work, i’ve occured something like this before but never this weird.

this isn’t a permanant answer, and i cant imagine this being the best one, but it works, if you’re still bothered about it, try asking someone else or smh

is that the script that loads the data is in charge of checking what is inside the table, and after searching it checks if the IDs that the table has match with any ID that any replicated storage tool has, since my tools I put an attribute that works like an ID

Try adding a BindToClose function. Could be that the game closes before it has time to save data.

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

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

There’s a couple of other things you might want to fix, too.

  1. FindFirstChild() should only be used when you are unsure something exists. Since this script is server-side, you don’t need to use it.

  2. Always use a pcall() when accessing the DataStore. It’s a network request and can fail.

Printing tables instead of addresses is a Roblox Studio feature, developer console does not have that. It’s normal.

I could not understand this one very well however if you are asking why your datastore has the same data on server and Roblox Studio, it is normal as well. Datastores are same no matter where in the same universe (if you add more places to a universe, they’ll also use the same datastores).

This is false. FindFirstChild is always the ideal way of referencing an instance’s child. The commonly used dot method (workspace.Part) is normally for indexing the properties but also works with children.

It does not save when exiting, it saves by pressing a gui button that I made

The problem is that when playing the game on the page it does not load the items in the same way as they load me playing in Roblox Studios

I just solved the problem, apparently I just had to put a wait(), I think something had simply not loaded and that’s why the script failed

game.Players.PlayerAdded:Connect(function(plr)
	wait(3)
	local Data = tools:GetAsync(plr.UserId)
	
	if Data ~= nil then
		
		print(Data)
		
		for i,v in pairs(Data) do
			
			if game.ReplicatedStorage.Items:FindFirstChild(i).Name == Data[i] then
				
				local clone = game.ReplicatedStorage.Items:FindFirstChild(i):Clone()
				clone.Parent = plr.Backpack
			end
		end
	else
		print("nil")
	end
end)

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