Help with making multiple data stores

Hey guys, I’ve been working on a aging system which requires me to save multiple data (age and seconds) I’ve tried so many things and many posts I’ve seen were not including pcalls, I’m really new to this datastore stuff so I’d appeciate if you can help me and explain why my script isn’t working/ how your solution works.

Also I’ve watched some tutorials and the dude was using module scripts (and I don’t know the reason) so maybe that’s why it was messing up?

I have 15 different versions of this script but I think this is the closest I got?

local module = {}

local DataStoreService = game:GetService("DataStoreService")
local ageDataStore = DataStoreService:GetDataStore("ageDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local Stats = Instance.new("Folder")
	Stats.Name = "Stats"
	Stats.Parent = player
	
	local age = Instance.new("IntValue")
	age.Name = "Age"
	age.Parent = Stats

	local seconds = Instance.new("IntValue")
	seconds.Name = "Seconds"
	seconds.Parent = age
	
	while wait(1) do	
		 seconds.Value = seconds.Value + 1
			if seconds.Value >= 3600
		then
			age.Value = age.Value + 1
				seconds.Value = 0
		end
	end
	

local data
local success, errormessage = pcall(function()
	data = ageDataStore:GetAsync(player.UserId.."-age","-seconds")
	end)
		if success and data then
			wait(2)
			player.Stats.Age.Value = data.age
			player.Stats.Age.Seconds.Value = data.seconds
			
		end
	
end)

game.Players.PlayerRemoving:Connect(function(player)

local data = {}
	local success, errormessage = pcall(function()
		ageDataStore:SetAsync(player.UserId.."-age","-seconds")
end)
	if success then
	data.age = player.Stats.Age.Value
	data.seconds = player.Stats.Age.Seconds.Value
		print ("Saved Data Successfully!")
	else
		print("Couldn't Save Data!")
		warn(errormessage)
	end
end)

	

return module
2 Likes

What’s the problem? Does the script not work? Are there errors?

2 Likes

You’re currently saving a string, “-seconds”.
By the looks of things, you’re wanting to save a dictionary with age set to the Player’s age likewise with seconds.

An example of this would be:

local Data = {age = player.Stats.Age.Value, seconds = player.Stats.Age.Seconds.Value};

This would then be passed to SetAsync as the second argument.

3 Likes

Everything works as intended except it doesn’t save/load anything.

Ahh, I understand now. It really looks obvious now that I know.
I’ll try that, thanks!

So it would be like this?

local module = {}

local DataStoreService = game:GetService("DataStoreService")
local ageDataStore = DataStoreService:GetDataStore("ageDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local Stats = Instance.new("Folder")
    Stats.Name = "Stats"
    Stats.Parent = player

    local age = Instance.new("IntValue")
    age.Name = "Age"
    age.Parent = Stats

    local seconds = Instance.new("IntValue")
    seconds.Name = "Seconds"
    seconds.Parent = age

    while wait(1) do
         seconds.Value = seconds.Value + 1
            if seconds.Value >= 3600
        then
            age.Value = age.Value + 1
                seconds.Value = 0
        end
    end


local data = {age = player.Stats.Age.Value, seconds = player.Stats.Age.Seconds.Value};
local success, errormessage = pcall(function()
    data = ageDataStore:GetAsync(player.UserId.."-age","-seconds")
    end)
        if success then
            wait(2)
            player.Stats.Age.Value = data.age
            player.Stats.Age.Seconds.Value = data.seconds

        end

end)

game.Players.PlayerRemoving:Connect(function(player)

local data = {age = player.Stats.Age.Value, seconds = player.Stats.Age.Seconds.Value};
    local success, errormessage = pcall(function()
        ageDataStore:SetAsync(player.UserId.."-age","-seconds")
end)
    if success then
    data.age = player.Stats.Age.Value
    data.seconds = player.Stats.Age.Seconds.Value
        print ("Saved Data Successfully!")
    else
        print("Couldn't Save Data!")
        warn(errormessage)
    end
end)



return module

Just a heads up, it still doesn’t work.

The probelem with your code is this:

ageDataStore:SetAsync(player.UserId.."-age","-seconds")

You’re saving the string -seconds like @ReturnedTrue said. You need to put the dictionary variable in there, not the scope of the DataStore:

ageDataStore:SetAsync(player.UserId.."-age", data) -- Notice how I changed "-seconds" to "data"

In addition, these 2 lines at the end of the code are meaningless, they don’t have a purpose in the code:

data.age = player.Stats.Age.Value
data.seconds = player.Stats.Age.Seconds.Value
1 Like

Pretty sure that’s the solution although I still couldn’t apply it to my script.

I guess I give up and will never mess with datastores, this is blowing my mind

We can still help you if you still need help. Just show us the script and any problems you are facing. Don’t give up, I’ve been through it. Trust me, it’s such a feeling going through it. In addition, I still sometimes have Datastore problems. So, think that other people are going through the same, it should help boost your confidence.

1 Like

I really appreciate what you said, I really needed that motivational boost. I’m sure everyone has been through all this but I just am someone with low confidence. Anyways, I don’t even remember where I left it off but last version of my script was:

local module = {}

local DataStoreService = game:GetService("DataStoreService")
local ageDataStore = DataStoreService:GetDataStore("ageDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local Stats = Instance.new("Folder")
	Stats.Name = "Stats"
	Stats.Parent = player
	
	local age = Instance.new("IntValue")
	age.Name = "Age"
	age.Parent = Stats

	local seconds = Instance.new("IntValue")
	seconds.Name = "Seconds"
	seconds.Parent = age
	
	while wait(1) do	
		 seconds.Value = seconds.Value + 1
			if seconds.Value >= 3600
		then
			age.Value = age.Value + 1
				seconds.Value = 0
		end
	end
	


local success, errormessage = pcall(function()
		data = {age = player.Stats.Age.Value, seconds = player.Stats.Age.Seconds.Value};
		data = ageDataStore:GetAsync(player.UserId.."-age", data)
	end)
		if success then
			wait(2)
			print("Loaded Data Successfully!")
	else
			print("Couldn't Load Data!")
			warn(errormessage)
		end
	
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
	data = ageDataStore:SetAsync(player.UserId.."-age", data)
end)
	if success then
		print ("Saved Data Successfully!")
	else
		print("Couldn't Save Data!")
		warn(errormessage)
	end
end)

	

return module

Only error I’m getting is argument 2 missing or nil at

warn(errormessage)

I have no idea as to what to put as a secondary argument in a pcall since I need it to save/load no matter what.

1 Like

This line:

data = ageDataStore:GetAsync(player.UserId.."-age", data)

You don’t put that as a variable, just do this:

ageDataStore:GetAsync(player.UserId.."-age", data)

In player removing function.

1 Like

Oh, I don’t even know when I put that there. My bad…
Though I’m still getting the same error at the playerremoving function it still wants me to put a secondary argument but I have no idea as to what I should put there as argument 2

Okay, so now I see 3 problems with your script:


Player removing function:

local success, errormessage = pcall(function()
	data = ageDataStore:SetAsync(player.UserId.."-age", data) -- Data is not defined
--   ^ this variable is not needed
end)

So you would fix it like this:

local data = {age = player.Stats.Age.Value, seconds = player.Stats.Age.Seconds.Value}
local success, errormessage = pcall(function()
	ageDataStore:SetAsync(player.UserId.."-age", data) -- Data is not defined
end)

And in the playerAdded function, the while loop should be wrapped in a coroutine:

local Coroutine
Coroutine = coroutine.create(function()
    while wait(1) do	
	    seconds.Value = seconds.Value + 1
			if seconds.Value >= 3600 then
			    age.Value = age.Value + 1
				seconds.Value = 0
            end
		end
	end
end)
coroutine.resume(Coroutine)

Basically a coroutine is a script inside a script
The second problem is this part:

data = {age = player.Stats.Age.Value, seconds = player.Stats.Age.Seconds.Value}; -- This is not needed
data = ageDataStore:GetAsync(player.UserId.."-age", data) -- the "data" variable in the second parameter is not needed

To fix it you can use this:

local data = ageDataStore:GetAsync(player.UserId.."-age")

Thirdly, you don’t need this to be a module script, but if it’s for other purposes then you can.


This is what the script would look like:

local module = {}

local DataStoreService = game:GetService("DataStoreService")
local ageDataStore = DataStoreService:GetDataStore("ageDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local Stats = Instance.new("Folder")
	Stats.Name = "Stats"
	Stats.Parent = player
	
	local age = Instance.new("IntValue")
	age.Name = "Age"
	age.Parent = Stats

	local seconds = Instance.new("IntValue")
	seconds.Name = "Seconds"
	seconds.Parent = age
	
	local Coroutine
	Coroutine = coroutine.create(function()
		while wait(1) do	
			 seconds.Value = seconds.Value + 1
				if seconds.Value >= 3600
			then
				age.Value = age.Value + 1
					seconds.Value = 0
			end
		end
	end)
	coroutine.resume(Coroutine)
	
	local Data
	local success, errormessage = pcall(function()
		data = ageDataStore:GetAsync(player.UserId.."-age")
	end)
	if success then
		wait(2)
		print("Loaded Data Successfully!")
	else
		print("Couldn't Load Data!")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

    data = {age = player.Stats.Age.Value, seconds = player.Stats.Age.Seconds.Value}
	local success, errormessage = pcall(function()
		ageDataStore:SetAsync(player.UserId.."-age", data)
	end)

	if success then
		print ("Saved Data Successfully!")
	else
		print("Couldn't Save Data!")
		warn(errormessage)
	end
end)

return module
1 Like

Finally! These were the reason why I wasn’t getting the

print("Loaded Data Successfully!")

output.

So most of the problems were variables and after fixing them all I needed to do was add

age.Value = data.age
seconds.Value = data.seconds

to the success of playeradded pcall and it all works now.
Now that I look at it, it looks really obvious why it wasn’t working.

Thanks alot for everything, you’re a true hero!

1 Like