I cant save or create a datastore

This is my second post today but i am making a game and a inventory system.I just decided to use datastores because its just gonna store int values…

But it doesnt work and i dont really know why it doesnt…

By The Way this is the whole script :

local dss = game:GetService("DataStoreService")
local datastore  = dss:GetDataStore("theDiceWip")
local invstore  = dss:GetDataStore("inventory")

local function ds(plr)
	--save data
	local savetable = {
		plr.leaderstats.Stars.Value;
		plr.leaderstats.Rolls.Value
	}
	
	local success,errorMessage = pcall(datastore.SetAsync,datastore,plr.UserId,savetable)
	
	if success then
		print("Data of player "..plr.UserId.." has been saved.")
	else
		print("Failed to save the data of the player"..plr.UserId..".")
	end
end

local function ds2(plr)
	local invtable = {
		plr.PlayerGui.invgui:WaitForChild("common").Value;
		plr.PlayerGui.invgui:WaitForChild("rare").Value;
		plr.PlayerGui.invgui:WaitForChild("legendary").Value;
		plr.PlayerGui.invgui:WaitForChild("superior").Value;
		plr.PlayerGui.invgui:WaitForChild("craziest").Value;
		plr.PlayerGui.invgui:WaitForChild("maniack").Value;
		plr.PlayerGui.invgui:WaitForChild("qmqmqm").Value
	}
	
	local success,errorMessage = pcall(datastore.SetAsync,invstore,plr.UserId,invtable)
	
	if success then
		print("Inventory of player "..plr.UserId.." has been saved.")
	else
		print("Failed to save the inventory of the player"..plr.UserId..".")
	end
end

--get leaderboard and datastore
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Wait()
	local leaderstats = Instance.new("Folder" ,plr)
	leaderstats.Name = "leaderstats"
	
	local stars = Instance.new("IntValue",leaderstats)
	stars.Name = "Stars"
	
	local rolls = Instance.new("IntValue",leaderstats)
	rolls.Name = "Rolls"
	
	local data = nil
	
	local invdata = nil
	
	local success, errorMessage = pcall(function()
		data = datastore:GetAsync(plr.UserId)
	end)
	
	local success2, errorMessage2 = pcall(function()
		invdata = invstore:GetAsync(plr.UserId)
	end)
	
	if success and data then
		stars.Value = data[1]
		rolls.Value = data[2]
	else
		print("The player ".. plr.UserId.. " has no data.")
		warn(errorMessage)
	end
	
	if success2 and invdata then
		plr.PlayerGui.invgui.common.Value = invdata[1]
		plr.PlayerGui.invgui.rare.Value = invdata[2]
		plr.PlayerGui.invgui.legendary.Value = invdata[3]
		plr.PlayerGui.invgui.superior.Value = invdata[4]
		plr.PlayerGui.invgui.craziest.Value = invdata[5]
		plr.PlayerGui.invgui.maniack.Value = invdata[6]
		plr.PlayerGui.invgui.qmqmqm.Value = invdata[7]
	else
		print("The player "..plr.UserId.." has no inventory data.")
		warn(errorMessage2)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds(plr)
	ds2(plr)
end)

game:BindToClose(function()
	for _, plr in ipairs(game.Players:GetPlayers()) do
		task.spawn(ds,plr)
		task.spawn(ds2,plr)
	end 
end)

if you can please help…

its not very important but i really want it to work…

:slight_smile:

4 Likes

Anything in the output?
Publish it and try it ingame

1 Like

No,there is no errors in output but i will still send the output.

there it is :

1 Like

Publish and try it ingame, not in studio

1 Like

Here is the client sided output :

image

And this is the server output :

image

no errors at all.

do not consider the “FreeSuperior …”
its not relevant to this script.

PS - The Rolls And Stars do work… only the inventory does not work

Remove the WaitForChild when saving. They aren’t necessary since you won’t be saving before they’ve loaded

When i was creating this script at first it was actually not “WaitForChild” and it still didnt work

1 Like

What are the values in

I don’t see any other problems

they are all just defaulted to zero.nothing else

Print the invtable before it’s saved and check the output

it gives me this table

{
[1] = 0,
[2] = 0,
[3] = 0,
[4] = 0,
[5] = 0,
[6] = 0,
[7] = 0
}

What does this return???

if it saves data successfully it will return the one in top or “Inventory of player UserID has been saved”

If it failed it says “Failed to save the inventory of the player UserID.”

if you meant something different tell me because i just understand it this way…

im kinda new to datastores and luau…like a few months or smth

What does the output print…? 30charrr

1 Like

it says :

btw you can use the empty character “” it doesnt get detected and can provide enough length…

I’m having issues with my game related to datastores as well. Players data is being wiped upon leaving the game. I believe the datastoreservice is having some issues…?

i guess

i guess i will recode the whole game​:skull::skull::skull:

1 Like

All you gotta do to fix this issue is first

    1. Copy the working script
    1. Modify copied script
    1. Done!

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