Alright, so this is a "free-modelled" tool persistance that saves/load on command. (im sure youve seen it before). Now im been trying to make this code to automatically save when someone enters and leaves, however it doesnt seem to work. There are no errors, and i cant tell whats going wrong, whether its the save or the load (on exit/enter). This is the original script, i was wondering if someone could help make it auto load on enter, and save on leave? ~thx, bubs
forceSaveCommand = "save/"
forceLoadCommand = "load/"
clearStarterGearOnLoad = true
dataReadyWaitTimeout = 8
saveSuccessMessage = "Your tools have been saved"
loadSuccessMessage = "Your tools have been loaded"
loadErrorMessage = "Your tools could not be loaded. Chat \"" .. forceLoadCommand .. "\" to retry."
saveErrorMessage = "Your tools could not be saved. Chat \"" .. forceSaveCommand .. "\" to retry."
loadFastMessage = "You can only load your tools every 3 minutes."
saveFastMessage = "You can only save your tools every 30 seconds."
dpPrefix = "SavedTools-0-"
--Debug mode
debug = false
save_times = {}
load_times = {}
debris = game:GetService("Debris")
function tellPlayer(player, text, time) -- messaging system
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)--ive taken this out before, and it still doesnt work.
playerLoad(player)
end
function onPlayerLeft(player)
local s, e = pcall(function () -- pcall is the only thing i dont understand in the code?
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)
