Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i fix the following problems of items not clone and/or not saving?

 local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")

local playerdata = DataStoreService:GetDataStore("testdata")

local tools = ServerStorage.Tools
local inventories = ServerStorage.Inventories

Players.PlayerAdded:Connect(function(client)
	local key = "client_" .. client.UserId
	local inventory
	local err, su = pcall(function()
		inventory = playerdata:GetAsync(key)
	end)
	
	if su then
		inventory = playerdata:GetAsync(key)
	else
		print("Failed to get datastore")
		script.Parent.SendError:Fire(client.UserId, client.Name, err)
		game:GetService("TeleportService"):Teleport(*insert place id here*, client)
	end
	
	local inventory_folder = Instance.new("Folder")
	inventory_folder.Name = client.Name
	inventory_folder.Parent = inventories

	for _, name in ipairs(inventory or { }) do
		local tool = tools[name]
		print(tool.Name)
		tool:Clone().Parent = client.Backpack
		tool:Clone().Parent = inventory_folder
	end
end)

Players.PlayerRemoving:Connect(function(client)
	local key = "client_" .. client.UserId
	local tools = { }
	local bkey = "backup_" .. client.UserId
	local inventory_folder = inventories[client.Name]

	for _, item in ipairs(inventory_folder:GetChildren()) do
		table.insert(tools, item.Name)
	end
	playerdata:UpdateAsync(key, function(prev)
		playerdata:SetAsync(bkey, prev)
		return tools
	end)

	inventory_folder:Destroy()
end)

local remote = game:GetService("ReplicatedStorage").Buy

local ServerStorage = game:GetService("ServerStorage")
local tools = ServerStorage.Tools
local inventories = ServerStorage.Inventories

remote.Event:Connect(function(client, request)
	local inventory_folder = inventories[client.Name]
	local tool = tools[request]
	if inventory_folder:FindFirstChild(tool.Name) == nil then
	tool:Clone().Parent = client.Backpack
	tool:Clone().Parent = inventory_folder	
	end
end)

basically the error function is supposed to report errors and send the player to a limbo housing, i need to clone the item to the players inventory and i cant clone and save the data even with manual cloning. I cant seem to identify the problems

1 answer

Apparently it only happened with single player servers and i was able to fix it with game:BindtoClose

This discussion is preserved as it was. New answers, comments, and votes are closed.