For many hours now I've been trying to figure out how to save tools in a players inventory upon them leaving, and how to load their saved tools upon them joining. I'm fairly new to scripting, so I was looking off of other scripts, while also trying to make my own. But nothing to far has worked. Any help would be greatly appreciated. Here is what I have, but nothing loads/saves, and there are no errors in the output.
dpPrefix = "SavedTools-2-"
debug = false
save_times = {} load_times = {}
debris = game:GetService("Debris")
function tellPlayer(player, text, time) if not player.Parent then return end local m = Instance.new("Message", player:findFirstChild("PlayerGui")) m.Text = text debris:AddItem(m, time or 3) end
function waitForDataReady(player) local start = tick() while tick() < start + dataReadyWaitTimeout do if player.DataReady then return true end wait() end return false end
function playerSave(player) if not waitForDataReady(player) then tellPlayer(player, saveErrorMessage) return end
if (save_times[player] or 0) + 30 > tick() then tellPlayer(player, saveFastMessage) return end save_times[player] = tick() local gear = player:findFirstChild("StarterGear") if not gear then tellPlayer(player, saveErrorMessage) return end local tools = gear:GetChildren() local worked = false pcall(function () player:SaveNumber(dpPrefix .. "Count", #tools) worked = true end) if not worked then tellPlayer(player, saveErrorMessage) return end for i, tool in pairs(tools) do pcall(function () player:SaveInstance(dpPrefix .. tostring(i), tool) end) end tellPlayer(player, saveSuccessMessage)
end
function playerLoad(player) if not waitForDataReady(player) then tellPlayer(player, loadErrorMessage) return end
if (load_times[player] or 0) + 60*3 > tick() then tellPlayer(player, loadFastMessage) return end load_times[player] = tick() local gear = player:findFirstChild("StarterGear") local backpack = player:findFirstChild("Backpack") local current = gear:GetChildren() local count = 0 pcall(function () count = player:LoadNumber(dpPrefix .. "Count") end) local tools = {} for i = 1, count do pcall(function () tools[#tools + 1] = player:LoadInstance(dpPrefix .. tostring(i)) end) end for i, tool in pairs(tools) do local copy = tool:clone() copy.Parent = backpack local copy2 = tool:clone() copy2.Parent = gear end if clearStarterGearOnLoad then for i, tool in pairs(current) do tool:remove() end end tellPlayer(player, loadSuccessMessage)
end
function onPlayerChatted(player, message, recipient) message = message:lower() if message == forceSaveCommand then playerSave(player) elseif message == forceLoadCommand then local s, e = pcall(function () playerLoad(player) end) if not s then if debug then tellPlayer(player, "playerLoad error:" .. e) end end end end
function onPlayerEntered(player) player.Chatted:connect(function (msg, rec) onPlayerChatted(player, msg, rec) end) playerLoad(player) end
function onPlayerLeft(player) local s, e = pcall(function () playerSave(player) end) if not s then if debug then Instance.new("Message", workspace).Text = "playerSave error:" .. e end end end
game.Players.PlayerAdded:connect(onPlayerEntered) game.Players.PlayerRemoving:connect(onPlayerLeft)
You appear to be using the old method of saving information. You should be using DataStores - the manual is here and provides sample code. Of note, you cannot save/load tools using this method. Instead, assign a unique name or number to each tool, convert the list of tools the player has to a list of names/numbers, and save that list. (When you load it, you go through the reverse process to give the players their tools back.)