Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Made By gangman67
- local DS = game:GetService("DataStoreService"):GetDataStore("DataTest")
- local WS = game:GetService("Workspace")
- local SS = game:GetService("ServerStorage")
- local RS = game:GetService("ReplicatedStorage")
- local Plrs = game:GetService("Players")
- local HS = game:GetService("HttpService")
- --These are Your Default Stats
- -- All data will be put into a folder found it replicatedStorage
- local DefaultStats ={
- "Level:1";
- "Exp:1";
- }
- local function isNumbers(input) -- Checks if the data is a number
- return (tonumber(input) and true or false) ii if so return true
- end
- function DataHandle(Data, Table) -- this splits "DataName:Value: and makes a value depending if number or string
- local FindData = ""
- local NewData = ""
- for tablen, Var in pairs(Table) do
- for i = 1, string.len(Var) do
- if Var:sub(i,i) == ":"then
- FindData = Var:sub(1,(i-1))
- NewData = Var:sub((i+1),string.len(Var))
- end
- end
- if isNumbers(NewData) == true then
- local val = Instance.new("NumberValue",Data)
- val.Name = FindData
- val.Value = NewData
- elseif isNumbers(NewData) == false then
- local val = Instance.new("StringValue",Data)
- val.Name = FindData
- val.Value = NewData
- end
- end
- end
- game.Players.PlayerRemoving:connect(function(Plr) --Saves plr data when they leave
- local key = "Data_"..Plr.UserId
- local DataTable ={}
- local data = RS:WaitForChild("Data_"..Plr.UserId)
- for DataN, Data in pairs(data:GetChildren()) do
- if Data:IsA("NumberValue") or Data:IsA("StringValue") then
- table.insert(DataTable, Data.Name..":"..Data.Value)
- end
- end
- local newData= HS:JSONEncode(DataTable)
- DS:SetAsync(key, newData)
- end)
- game.Players.PlayerAdded:connect(function(Plr) --Loads and configured data when they join
- local Key = "Data_"..Plr.UserId
- local DummyData = DS:GetAsync(Key)
- local DataF = Instance.new("Folder", RS)
- DataF.Name = "Data_"..Plr.UserId
- if DummyData ~= nil then
- local StoredData = HS:JSONDecode(DummyData)
- DataHandle(DataF, StoredData)
- else
- DataHandle(DataF, DefaultStats)
- end
- end)
- game.OnClose = function() -- keeps the server open 5 seconds after its shutdown
- for I,v in pairs(game.Players:GetChildren()) do -- this kicks the players which will fire the save function
- v:Kick("The Server Has shutdown we saved your data") --Msg the player when they leave
- wait(.1)
- end
- wait(5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement