Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my data saving script not working? I cannot figure out what is wrong with the code I wrote.

I've been trying to fix this script for quite a while now and I cannot find a fix for it. I have no idea what is wrong with the code. Every time I load my data it does not save.

local MyDatastore = game:GetService("DataStoreService"):GetDataStore("MyDatastore")
local IntValues = {
	["Cash"] = 0;
}

local BoolValues = {
	["TestBool"] = false;
}

local StringValue = {
	["TestString"] = "";
}

game.Players.PlayerAdded:Connect(function(Player)
	local DataFolder = Instance.new("Folder",Player)
	DataFolder.Name = "leaderstats"
	
	local Inventory = Instance.new("Folder",Player)
	Inventory.Name = "Inventory"
	
	for i,v in pairs(IntValues) do
		local Value = Instance.new("IntValue",DataFolder)
		Value.Name = i
	end
	
	for i,v in pairs(BoolValues) do
		local Value = Instance.new("BoolValue",DataFolder)
		Value.Name = i
	end
	
	
	for i,v in pairs(StringValue) do
		local Value = Instance.new("StringValue",DataFolder)
		Value.Name = i
	end
	
	local Data
	
	local Success,Fail = pcall(function()
		Data = MyDatastore:GetAsync(Player.UserId)
	end)
	if Success then
		print("Successfully loaded "..Player.Name.."'s data!")
		for i,v in pairs(Data) do
			for index,value in pairs(DataFolder:GetChildren()) do
				if v[1] == value.Name then
					if type(v[2]) == "number" then
						v[2] = value.Value
						print(v[1],v[2])
					end
					if type(v[2]) == "string" then
						v[2] = value.Value
						print(v[1],v[2])
					end
					if type(v[2]) == "boolean" then
						v[2] = value.Value
						print(v[1],v[2])
					end
				end
			end
		end
	elseif Fail then
		for i,v in pairs(DataFolder) do
			if v:IsA("IntValue") then
				v.Value = 0
			end
			if v:IsA("StringValue") then
				v.Value = ""
			end
			if v:IsA("BoolValue") then
				v.Value = false
			end
		end
		warn("Did not successfully load "..Player.Name.."'s data!")
		
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	
	local SavingTable = {}
	
	for i,v in pairs(Player.leaderstats:GetChildren()) do
		local NewTable = table.create(1)
		table.insert(NewTable,1,v.Name)
		table.insert(NewTable,2,v.Value)
		table.insert(SavingTable,NewTable)
	end
	
	for i,v in pairs(SavingTable) do
		for index,value in pairs(v) do
			print(value)
		end
	end
	
	local Success,Fail = pcall(function()
		MyDatastore:SetAsync(Player.UserId,SavingTable)
	end)
	if Success then
		print("Saved "..Player.Name.."'s data!")
	elseif Fail then
		warn("Did not Save "..Player.Name.."'s data!")
	end
	
end)

1 comment

0

my answer worked?

1 answer

✓ Accepted answer

Ill tell you some thing that i guarantee works, stare at that code and anything else that may interact with it, and just think about it for at least 2 hours, then and only then will you realise your mistake, if not then just look up how to use everything and relearn it so THEN you can find ur mistake :>

If not of that works then jsut do some debugging, look at suspicous stuff, take it out temporarily (dont forget to copy it to ur clipboard so you can paste it back in), then put some print() lines in (always works with me no joke), find out what works and what doesnt print(), then stare at THAT code, then try some more testing with removing some code then adding it back in, eventually you will figure it out because im too lazy to read that whole thing myself

hope this helps :3

This discussion is preserved as it was. New answers, comments, and votes are closed.