New to DataStores, is this Right?

  1. I want to make it so if a player joins the game and contains the Data: “OwnedFactory” then it clones that factory to a location that isn’t currently being used.

  2. The issue is that its not doing the script at all and I’m new to DataStores so I am not sure if I did it right.

LOCATED IN “ServerScriptService” V

local dFactory = game:GetService("ReplicatedStorage").Factories.DefaultFactory
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("FactoryDS")
local Locations = game:GetService("Workspace").Locations:GetChildren()
local Data = DataStore:GetAsync(tostring(Players.LocalPlayer.UserId))
if Data and Data["OwnedFactory"] then
    for _, location in ipairs(Locations) do
        if not location:FindFirstChild("DefaultFactory") then
            local loadDFac = dFactory:Clone()
            loadDFac.Parent = location
            loadDFac:SetPrimaryPartCFrame(location.CFrame)
        end
    end
end

This Script is supposed to:

  • Retrieve necessary services and objects.
  • Fetch the local player’s data from a data store.
  • Iterate through all child objects under Workspace.Locations.
  • For each location, it checks if a DefaultFactory does not already exist.
  • If the player owns a factory (as indicated by the data store), it clones the DefaultFactory and places it at the location.

you can’t save objects through DataStore, only UTF - 8 characters, but you can use serialization, this is how tycoons save their games, if you have system like “ClaimPart” then add two BindableEvents , one will :Fire() when player touches the claim part whilst the other will Fire() to save tycoon using while wait() do function

local dts = game:GetService("DataStoreService")
local partdata = dts:GetDataStore("TycoonData")
local plrs = nil
local savingtable = {}

game.Players.PlayerAdded:Connect(function(plr)
	plrs = plr
	
	for _, event in workspace.Factory:GetDescendants() do
		if event:IsA("BindableEvent") and event.Name == "YourEventNameForClaimingTycoon" then
			event.Event:Connect(function()
				local model = event.Parent.Parent.Parent -- Use your reference to where BindableEvent is stored 
				
				local data
				local success, errormsg = pcall(function()
					data = partdata:GetAsync(plr.UserId)
				end)
				if success and data ~= nil then
					for i, v in pairs(data) do
						local part = Instance.new("Part", model.Purchases) -- You should add folder where all player bought items are
						part.Anchored = true
						part.Position = Vector3.new(v["PosX"], v["PosY"], v["PosZ"])
						part.Size = Vector3.new(v["SizeX"], v["SizeY"], v["SizeZ"])
						part.Color = Color3.fromRGB(v["ColorR"]*255, v["ColorG"]*255, v["ColorB"]*255)
						part.Material = v["Material"]
						part.Orientation = Vector3.new(v["OrX"], v["OrY"], v["OrZ"])
						part.BottomSurface = Enum.SurfaceType.Smooth
						part.TopSurface = Enum.SurfaceType.Smooth
					end
				end
			end)
			
		elseif event:IsA("BindableEvent") and event.Name == "Save" then
			event.Event:Connect(function()
				table.clear(savingtable)
				
				local model = event.Parent.Parent.Parent 
				
				for i, v in pairs(model:FindFirstChild("Purchases"):GetDescendants()) do
					if v:IsA("BasePart") then
						local NewTable = {
							PosX = v.Position.X, 
							PosY = v.Position.Y, 
							PosZ = v.Position.Z,
							OrX = v.Orientation.X,
							OrY = v.Orientation.Y,
							OrZ = v.Orientation.Z,
							SizeX = v.Size.X,
							SizeY = v.Size.Y,
							SizeZ = v.Size.Z,
							ColorR = v.Color.R,
							ColorG = v.Color.G,
							ColorB = v.Color.B,
							Material = v.Material.Name
						}
						table.insert(savingtable, NewTable)
					end
				end
			end)
		end
	end
end)

game.Players.PlayerRemoving:Connect(function()
	partdata:SetAsync(plrs.UserId, savingtable)
end)

game:BindToClose(function()
	wait(10)
end)

It’s not saving an Object into DataStore Service. Its checking if the Players Data contains “OwnedFactory” and then Clones an Object from ReplicatedStorage into the Workspace.