Basically we i join then quit and then rejoin the data dosen't get saved and then when I rejoin for the 3rd time its saved, whats wrong

-- Define
local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TycoonSave")

-- Save function (will explain more in the function)
local function Save(plr)
	local key = "plr-"..plr.UserId -- we need to have a key for each player
	
	local saveFolder
	local saveTable = {}
	for i,v in pairs(workspace.Bases:GetChildren()) do
		if v.Owner.Value == plr.Name then
			saveFolder = v.Placed
		end
	end
	
	local objects = saveFolder:GetChildren() -- all the objects
	
	-- Loops through all the objects in the saveFolder
	for i, obj in pairs(objects) do
		if obj:IsA("Model") then
			-- Inserts the name and position of the model
			table.insert(saveTable, {
				["Name"] = obj.Name;
				["CFS"] = { -- Positions are a bit complex
					["X"] = obj.PrimaryPart.CFrame.X;
					["Y"] = obj.PrimaryPart.CFrame.Y;
					["Z"] = obj.PrimaryPart.CFrame.Z;
					["ROT"] = obj.PrimaryPart.Orientation.Y
				}
			})
			
			-- This is simple though we can access this again by saying saveTable.CFS.AXIS
		end
	end
	
	local success, err = pcall(function() -- prevents script from stopping
		DataStore:SetAsync(key, saveTable) -- Saves the table
	end)
	
	-- If the saving failed
	if not success then
		warn("Failed to over-write data"..tostring(err))
		return
	end
	
	pcall(function()
		DataStore:SetAsync("u_"..plr.userId, plr.RealMoney.Value)
		print("Saved")
	end)
	
	for i,v in pairs(workspace.Bases:GetChildren()) do
		if v.Owner.Value == plr.Name then
			local clonedbase = game.ReplicatedStorage[v.Name]:Clone()
			workspace[v.OreFolderName.Value]:Destroy()
			v:Destroy()
			clonedbase.Parent = workspace.Bases
		end
	end
end

-- Load function (will explain more in the function)
local function Load(plr)
	repeat wait(.1) until game.Players[plr.Name]
	
	local key = "plr-"..plr.UserId -- we need the players key so we can access their data
	
	local savedTycoon -- We will set this in a pcall()
	
	local tycoon
	
	for i,v in pairs(workspace.Bases:GetChildren()) do
		if v.Owner.Value == plr.Name then
			tycoon = workspace.Bases[v.Name]
		end
	end
	
	local success, err = pcall(function()
		savedTycoon = DataStore:GetAsync(key) -- reads the data saved
	end)
	
	-- if error
	if not success then
		warn("Failed to read data"..tostring(err))
		return
	end
	
	pcall(function()
		local store = DataStore:GetAsync("u_"..plr.userId)
		if store then
			print("Found cash data")
			plr.RealMoney.Value = store
		else
			print("No data found")
			--ds:SetAsync("u_"..p.userId, rc.Value)
		end
	end)
	
	-- If there was no error and if there is data to be loaded
	if savedTycoon then
		wait(1)
		
		for i, obj in pairs(savedTycoon) do
			if obj then
				-- We can use the saved data to access the name: obj.Name will give us the name
				local savedModel = game.ReplicatedStorage.Items:FindFirstChild(obj.Name):Clone()
				
				if savedModel then
					-- Positions the model
					local addX = 0
					local addZ = 0
					if tycoon.Name == "Red" then
						addX = 372
						addZ = -372
					elseif tycoon.Name == "Green" then
						addZ = -372
					elseif tycoon.Name == "Yellow" then
						addX = 372
					end
					savedModel.PrimaryPart = savedModel.Hitbox
					savedModel:SetPrimaryPartCFrame(CFrame.new(obj.CFS.X+addX, obj.CFS.Y, obj.CFS.Z+addZ) * CFrame.Angles(0, math.rad(obj.CFS.ROT), 0))
					savedModel.PrimaryPart.Transparency = 1
					
					--[[
					for index, value in pairs(savedModel:GetDescendants()) do
						if value then
							if value:IsA("BasePart") then
								value.CanCollide = true
								value.Transparency = 0
							end
						end
					end
					--]]
					
					savedModel.PrimaryPart.CanCollide = false
					savedModel.Parent = tycoon.Placed
					local hitbox = savedModel.PrimaryPart:Clone()
					hitbox.Parent = tycoon.Placed.Hitboxes
					savedModel.HitboxObj.Value = hitbox
				end
			end
		end
	else
		Save(plr)
	end
end

-- Calls
players.PlayerAdded:Connect(Load)
players.PlayerRemoving:Connect(Save)

3 comments

0

You already asked this question today. Update your previous one if you found something new, don't make an identical question within the same day as your original question.

0

It got removed

0

Oh that's odd, you should report that in the SH discord.