I don't know how to make this datastore system

Intro

Hi , I’m having trouble figuring out how to do a system that makes the player wait to achieve a build so the player needs to wait to finish his build and I’m trying to make it work offline. (the player can have multiple builds)

Issue

The issue is that when I start the game everything is ok and it subtracts the time that the player has been online from the time left for the build to be completed.BUT it reset the timer to its original value (the timer is a NumberValue that every build have in them and the value)

What I tried

I tried making datastores to save the time left for each building but I couldn’t achieve it…

More Info

(also the number value decrease every second and when the timer == 0 then the game consider the build achieve)

Source Code

Here the source code :

local DataStore = game:GetService("DataStoreService"):GetDataStore("Build")

local hourWait = 0


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

	local timeNow = os.time()

	local data

	pcall(function()
		data = DataStore:GetAsync(player.UserId.."-build")
		print("Getting Data")
	end)

	if data ~= nil then
		-- Returning player to the game

		local timeSinceLastClaim = timeNow - data -- Number in seconds since the last reward was claimed

		print("Time since last claim "..timeSinceLastClaim)

		if(timeSinceLastClaim / 60) >= hourWait then
			-- They are eligible for a reward

			wait(10)
			local Mines = game.Workspace.Plots.Plot1.ItemHolder:GetChildren()

			for i, v in pairs(Mines) do
				if v then
					if v:FindFirstChild("Builded") then
						if v.Builded.Value == false then
							if v.Timer.Value <= timeSinceLastClaim then
								v.Timer.Value = 0
							else
								v.Timer.Value = v.Timer.Value - timeSinceLastClaim
							end
						end
					end
				end			
			end


			DataStore:SetAsync(player.UserId.."-build",os.time())
		else
			DataStore:SetAsync(player.UserId.."-build",os.time())
		end
	else
		-- New player
		print("New player")
		DataStore:SetAsync(player.UserId.."-build",os.time())
	end
end)

I just want to know how I would be able to make this.

1 Like

If I’m correct, DataStores uses an API to connect to roblox’s servers, so you can’t use it offline. You’d be better off with an IntValue or the _G global variable.

Oops sorry, I explained it badly :slight_smile:

actually, I don’t use datastore when the player is offline I used them to save the Minute the second and the days the player visited then use this t know how much time the player has been offline.

but I don’t really know how to make a system to save every timer in every building.

First off I suggest you use DataStore2 because its super simple to save and load dictionaries

Second, just use _G and have all the player data in a server script on the server in a playeradded event

_G.PlayerData = {}

game.Players.PlayerAdded:Connect(function(player)
	_G.PlayerData[player.UserId] = {
		
		["Builds"] = {
			
			["Build1"] = {
				
				timerValue = 230
				
			};
			
			["Build2"] = {
				
				timeValue = 500
				
			};
			
			["Build3"] = {
				
				timerValue = 230
					
			}
			
			
		}
		
		
		
		
	}
1 Like

To be more precise I need to save the timer when the player disconnect so when the player reconnect I can properly substract the time on the timer.(Right now when a player join the game (if he have a building in progress) the game will just reset the timer then substract the time that the player have been offline.

oh ok, try this. thanks :slight_smile:

1 Like

Also if I have an undertermined amount of building what should I do ?

wat do you mean undetermined amount of buildings, its so simple

every time you place a build put it into the builds table, everytime a build is removed search the Builds table index to find a match and then destroy it

When you add the builds into a table, remember its a dictionary so you can do something like this

function addBuild(buildName)
	_G.PlayerData[player.UserId]["Builds"][buildName] = {}
end
1 Like

Im creating an index which is the name of the build and then I set that to an empty table

1 Like

But in your case your probably gonna wanna put like a timer value or whatever

Also remember that you can only save integers, strings, dictionaries, tables and booleans

@jeanpaupaul

did you actually get it to work or to you somewhat understand what i was trying to say

1 Like