Help with DataSave

I created a script that should save purchased items in a folder. And it seems to work in Roblox Studio, but in game it doesn’t work and gives an error. I tried to use the function waitforchild, but it doesn’t work either.

script

local Players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("SaveMyData")

function saveData(player)
	local tycoon = player:FindFirstChild("TycoonOwned").Value
	local purchases = player:FindFirstChild("TycoonOwned").Value.Purchases
	local tycoonData = {}

	for i, object in ipairs(purchases:GetChildren()) do
		table.insert(tycoonData, object.Name)
	end

	local success, Error = pcall(function()
		dataStore:SetAsync(player.UserId, tycoonData)
	end)
	if not success then
		warn(Error)
	end

	local newTycoon = game.ServerStorage:FindFirstChild(tycoon.Name):Clone()
	newTycoon.Parent = tycoon.Parent
	tycoon:Destroy()

end

local function Buttons(button)
	button.Button.CanCollide = false
	button.Button.Transparency = 1
	button.Button.BillboardGui.Enabled = false
end

script.Parent.ClaimTycoon.Event:Connect(function(tycoon)
	local tycoonOwner = tycoon.Values.OwnerValue.Value
	local tycoonData

	local success, Error = pcall(function()
		tycoonData = dataStore:GetAsync(tycoonOwner.UserId)
	end)
	if not success then
		warn(Error)
	end

	if success and tycoonData then
		local tycoonClone = game.ServerStorage:FindFirstChild(tycoon.Name):Clone()
		tycoonClone.Parent = tycoon.Parent
		tycoonClone.Values.OwnerValue.Value = tycoon.Values.OwnerValue.Value
		tycoonOwner.TycoonOwned.Value = tycoonClone
		tycoonClone.MainItems.OwnerDoor.Title.SurfaceGui.TextLabel.Text = tycoon.MainItems.OwnerDoor.Title.SurfaceGui.TextLabel.Text
		tycoonClone.MainItems.OwnerDoor.Door.Transparency = 1
		tycoon:Destroy()
		tycoon = tycoonClone

		local purchasesFolder = tycoonClone:FindFirstChild("Purchases")
		local purchasedItemsFolder = tycoonClone:FindFirstChild("PurchasedItems")
		local buttonsFolder = tycoonClone:FindFirstChild("Buttons")

		for i, button in ipairs(buttonsFolder:GetChildren()) do
			if button:FindFirstChild("Object") then
				local object = purchasedItemsFolder:FindFirstChild(button.Object.Value)
				if object and table.find(tycoonData, object.Name) then
					object.Parent = purchasesFolder
					Buttons(button)
				end
			end
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)

game:BindToClose(function()
	for i, player in ipairs(game.Players:GetPlayers()) do
		saveData(player)
	end
end)

I’ve also been having a lot of problems with FindFirstChild and WaitForChild recently, despite not having any in the past. I find putting task.wait(1) at the start of the script, before anything else, generally fixes any issues. Probably not the best practice, but it does work.

1 Like

Assuming you’re calling saveData() when the player leaves, I feel it’s safe to assume that there was enough time given for TycoonOwned to exist and be parented under the player.

Double check that the script that’s creating TycoonOwned is working correctly and parenting under the correct place.

(or whatever is assigning the value)

1 Like

why are you trying to get “Purchases” from the value of “TycoonOwned”?

2 Likes

i tried it and … yes, it works. Thank you! and thanks to everyone who wanted to help!

1 Like

Ohh, I was contemplating if it was just a child. That makes more sense. Thanks for the response!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.