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

dataStores DONT SAVE DATA?!

I was testing my DataStore script and I noticed the data wasn't saving. The only things printing are: "GenuineWaffles has no data." and basically just the stat values that are in a table called "Data"

It never saves and it's killing me.

Here is the script:


--< Variables >--

local DataStoreService = game:GetService('DataStoreService')
local ServerStorage = game:GetService('ServerStorage')

local MainModules = ServerStorage:WaitForChild('Main - Modules')
local DataStore = DataStoreService:GetDataStore('test')

local DataModule = require(MainModules:WaitForChild('Data - Module'))

local Stats = {
	'NeededXP';
	'Level';
	'Coins';
	'XP';
}

local DefaultValues = {
	['NeededXP'] = 100;
	['Level'] = 1;
	['Coins'] = 300;
	['XP'] = 0;
}

--< Functions >--

local function OnPlayerAdded(NewPlayer)
	DataModule.CreateData(NewPlayer,Stats,DefaultValues)
	DataModule.LoadData(NewPlayer,DataStore)
end

local function OnPlayerLeaving(OldPlayer)
	DataModule.SaveData(OldPlayer,DataStore,Stats)
end

--< Main - Program >--

game:GetService('Players').PlayerAdded:Connect(OnPlayerAdded)

game:GetService('Players').PlayerRemoving:Connect(OnPlayerLeaving)

Here is the module:

--< Variables >--

local Module = {}

--< Functions >--

function Module.CreateKey(Player)
	return ('Key-'..Player.UserId)
end

function Module.CreateObject(Parent,Class,Name)
	local Object = Instance.new(Class)
	Object.Parent = Parent
	Object.Name = Name
	return (Object)
end

function Module.CreateData(Player,Stats,DefaultValues)
	local DataFolder = Module.CreateObject(Player,'Folder','DataFolder')
	for Index,Value in next, Stats do
		local Object = Module.CreateObject(DataFolder,'IntValue',Value)
		Object.Value = DefaultValues[Value]
	end
end

function Module.LoadData(Player,DataStore)
	local DataFolder = Player:WaitForChild('DataFolder')
	local Key = Module.CreateKey(Player)
	local Data
	local Success,Error = pcall(function()
		Data = DataStore:GetAsync(Key)
	end)
	if not (Error) then
		if (Data) then
			for Index,Value in (Data) do
				DataFolder[Value].Value = Data[Index]
			end
		else
			print(Player.Name..' has no data.')
		end
	else
		print(Error)
		Player:Kick('There was an error in processing your data.')
	end
end

function Module.SaveData(Player,DataStore,Stats)
	local DataFolder = Player:WaitForChild('DataFolder')
	local Data = {}
	local Key = Module.CreateKey(Player)	
	for Index,Value in next, Stats do
		table.insert(Data,DataFolder[Value].Value)
	end
	print(unpack(Data))
	DataStore:SetAsync(Key,Data)
	print('Saved')
end

--< Main - Program >--

return Module

It never works! If someone can help, please do. And if you need anymore information, please ask.

1 answer

✓ Accepted answer

I fixed it. Basically I forgot to put a "next" Sorry for the hassle.

1 comment

0

not a hasssle. glad you got it fixed

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