Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --StarterGear Save Script
- -- jaredvaldez2/jaredvaldez4
- forceSaveCommand = "save/"
- forceLoadCommand = "load/"
- autoToolSaveTime = 120 -- How many seconds until autosave
- clearStarterGearOnLoad = true
- dataReadyWaitTimeout = 8 -- How many seconds will it wait for dataready.
- saveSuccessMessage = "Tools saved!" -- save text
- loadSuccessMessage = "Tools loaded!" -- load text
- secondsL = 120 -- Seconds to load
- secondsS = 30 -- Seconds to save
- 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 2 minutes."
- saveFastMessage = "You can only save your tools every 15 seconds."
- dpPrefix = "SavedTools-0-" -- Don't change, unless you want to wipe all data from tool saves.
- 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) + secondsS > tick() then tellPlayer(player, saveFastMessage) return end
- save_times[player] = tick()
- local gear = player:findFirstChild("StarterGear")
- local tools = gear:GetChildren()
- local worked = false
- pcall(function ()
- player:SaveNumber(dpPrefix .. "Count", #tools)
- worked = true
- 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) + secondsL > 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)
- coroutine.wrap(function()
- while wait(autoToolSaveTime) do
- playerSave(player)
- end
- end)()
- 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement