|
| 09 Jul 2016 09:35 AM |
There's a folder in Workspace. It has string values in it. How do I make it so when the server is closing, all of these string values in the folder save and when a new server is created, these values spawn again in the folder?
|
|
|
| Report Abuse |
|
|
| |
|
|
| 09 Jul 2016 09:52 AM |
| You could loop through the children of the folder, and into an table, save the name and value of the StringValue. You can then serialise the table using the JSONEncode function of the HttpService. You can then save the serialised table into a DataStore on shutdown. When the game is next restarted, you can reload the string that was saved to the DataStore, deserialise it using JSONDecode, and loop through the deserialised table, recreating the StringValues using Instance.new. |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 09:53 AM |
Could I have a script please?
Thanks
|
|
|
| Report Abuse |
|
|
| |
|
|
| 09 Jul 2016 10:15 AM |
Idk why he was talking about JSONEncode. Here:
local Folder = script.Data --Change this to the path to the Folder local Saves = game:GetService("DataStoreService"):GetDataStore("Saves") --Change the name to whatever DataStore you're currently using for saving stuff
--Note: This only works for StringValues, if you want to save anything else this can be easily modded, but I will not be doing it for you
function Load() local Data = Saves:GetAsync(Folder.Name) for i,v in next, Data do local Value = Instance.new("StringValue", Folder) Value.Name = i Value.Value = v end end
function Save() local Data = {} for i,v in next, Folder:GetChildren() do Data[v.Name] = v.Value end Saves:SetAsync(Folder.Name, Data) end
Load()
game.OnClose = Save |
|
|
| Report Abuse |
|
|
| |
|
|
| 09 Jul 2016 10:17 AM |
-- Settings local Settings = { Folder_Location = workspace ;Folder_Name = "Folder" ;Folder_ClassName = "Folder" ;DataStore_Name = "SavedStringValues" ;DataStore_Key = "SavedStringValues" }
-- Variables used throughout the script local DataStoreService, HttpService = game:GetService("DataStoreService"), game:GetService("HttpService") local DataStore = DataStoreService:GetDataStore(Settings.DataStore_Name) local Folder = Settings.Folder_Location:FindFirstChild(Settings.Folder_Name)
-- This is called as soon as the script has loaded function OnGameLoad() -- Creates a new folder if it doesn't exist if (Folder == nil) then Folder = Instance.new(Settings.Folder_ClassName) Folder.Name = Settings.Folder_Name Folder.Parent = Settings.Folder_Location end -- Gets the saved data local SavedData = DataStore:GetAsync(Settings.DataStore_Key) -- Checks to see if the data actually exists if (SavedData ~= nil) then -- If data has been saved, decode it SavedData = HttpService:JSONDecode(SavedData) -- Only continue if the type of data decoded was a table if (type(SavedData) == "table") then -- Loop through each of the table entries for _,StringValueData in pairs(SavedData) do -- Create a new StringValue for each entry local NewStringValue = Instance.new("StringValue") NewStringValue.Name = StringValueData.Name NewStringValue.Value = StringValueData.Value NewStringValue.Parent = Folder end end end end
-- This is fired when the game shuts down game.OnClose = function() -- Declare a new table to insert the values local StringsTable = {} -- Loop through each of the children of the folder for _,Child in pairs(Folder:GetChildren()) do -- Only accept StringValues if (Child:IsA("StringValue")) then -- Add child to the StringsTable table table.insert(StringsTable, {Name=Child.Name;Value=Child.Value}) end end -- Encode the table as a JSON string StringsTable = HttpService:JSONEncode(StringsTable) -- Upload the JSON string to the DataStore DataStore:SetAsync(Settings.DataStore_Key, StringsTable) end
-- Fire the "OnGameLoad" function OnGameLoad() |
|
|
| Report Abuse |
|
|
| |
|
|
| 09 Jul 2016 11:27 AM |
Lol
Btw, I need help with something else
Can you possibly get a scrolling frame gui with text labels displaying all of the string values on it?
|
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 11:40 AM |
| That's just math, I won't be doing it for you, you can figure it out I'm sure |
|
|
| Report Abuse |
|
|
| |
|
|
| 09 Jul 2016 11:49 AM |
for _,StringValue in pairs(workspace.Folder:GetChildren()) do -- Every time it picks up a StringValue in the Folder, this part will run. -- This is where you will put the code to create a new button/label for each value. end
I'm not making another script though. This is just a key part you'll need. |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 11:51 AM |
| @clock Your script is entirely overkill |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 11:55 AM |
| It's commented so he can follow the process (and hopefully learn). It includes a settings table so he can change settings with ease. It also features data validation, to ensure errors don't arise in the script. It's more complicated and longer than yours, simply because it ensures a more stable approach. |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 11:57 AM |
That script is really hard to understand, I wanted to know how to get it all there
I already got a text button to show a scrolling frame gui when you clcik it, but I need to know how to get the text label's text the same as the bool vals
|
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 12:07 PM |
Our Scripts are equally stable. And yours contains far too many variables, to the point where it's more complicated and elaborate. Lemme explain why your Script is overkill:
--These settings are entirely useless and you'll see why as I comment other parts of the Script
local Settings = { Folder_Location = workspace ;Folder_Name = "Folder" ;Folder_ClassName = "Folder" ;DataStore_Name = "SavedStringValues" ;DataStore_Key = "SavedStringValues" }
local DataStoreService, HttpService = game:GetService("DataStoreService"), game:GetService("HttpService") --No need to define DataStorService you are only using it once. No need to define HttpService since you won't need it at all local DataStore = DataStoreService:GetDataStore(Settings.DataStore_Name) --Only using the DataStore name once, no need for a setting local Folder = Settings.Folder_Location:FindFirstChild(Settings.Folder_Name) --Just specify a correct path and it eliminates a lot of problems
function OnGameLoad() if (Folder == nil) then --There is no need to create a new Folder if you simply specify the path. Eliminates a lot of 'settings' Folder = Instance.new(Settings.Folder_ClassName) Folder.Name = Settings.Folder_Name Folder.Parent = Settings.Folder_Location end local SavedData = DataStore:GetAsync(Settings.DataStore_Key) if (SavedData ~= nil) then --If SavedData is false just put or {} after the GetAsync call SavedData = HttpService:JSONDecode(SavedData) --No need to use JSONDecode if you simply save it as an array if (type(SavedData) == "table") then --This check is uneccesary if you just save it as an array for _,StringValueData in pairs(SavedData) do local NewStringValue = Instance.new("StringValue") NewStringValue.Name = StringValueData.Name --Wasting space by saving the name in a table instead of using the index of SaveData NewStringValue.Value = StringValueData.Value --Wasting space by saving the value in a table instead of the value of the index in SaveData NewStringValue.Parent = Folder end end end end
game.OnClose = function() local StringsTable = {} for _,Child in pairs(Folder:GetChildren()) do if (Child:IsA("StringValue")) then --No need to do this check as all his values are StringValues table.insert(StringsTable, {Name=Child.Name;Value=Child.Value}) --Could just save StringsTable[Child.Name] = Child.Value as specified earlier in the loading end end StringsTable = HttpService:JSONEncode(StringsTable) --No need to JSONEncode when you could just save the table DataStore:SetAsync(Settings.DataStore_Key, StringsTable) end
OnGameLoad() |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 12:09 PM |
I used that guys script and yours. Yours never worked, but his (the longer script) did work.
|
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 12:10 PM |
Oops sorry, that's me on my brothers account
|
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 12:14 PM |
Lemme explain my side now
--These are the only 2 variables he has to mess with. The path to the Folder, and DataStore name he's using to save things. Whereas your Script goes way over the top with them local Folder = script.Data --Change this to the path to the Folder local Saves = game:GetService("DataStoreService"):GetDataStore("Saves") --Change the name to whatever DataStore you're currently using for saving stuff
--Note: This only works for StringValues, if you want to save anything else this can be easily modded, but I will not be doing it for you
--This function will be called when the game begins function Load() -- I get the values under the key using the name of the folder. To make it easy to identify and/or wipe local Data = Saves:GetAsync(Folder.Name) --Loading data is super easy because I save the StringValues in key, value pairs corresponding to the name and value. It's all the information I need, and saves space in the DataStore for if he needs to save more values (rather than your approach which wastes space with nested tables) local Value = Instance.new("StringValue", Folder) Value.Name = i Value.Value = v end end
--This function is called when the game closes function Save() --I define a table to store values in local Data = {} --And for each child I save a key,value pair corresponding to the name and value of that StringValue. It's all the information I need, and saves space in the DataStore for if he needs to save more values (rather than your approach which wastes space with nested tables) for i,v in next, Folder:GetChildren() do Data[v.Name] = v.Value end --I set the data to the DataStore under the key of the folder's name, to make it easily identifiable and wipe Saves:SetAsync(Folder.Name, Data) end
--Call the load function Load()
--Hook up the save callback game.OnClose = Save |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 12:17 PM |
| @Mun I can't fix it if you don't tell me the error |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 12:18 PM |
It's ok, I found out how to check the results (Names of string vales in the folder)
Thanks guys!
|
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 12:19 PM |
Oh hold on, I forgot to add a default value as an empty table. Try this lol
local Folder = script.Data --Change this to the path to the Folder local Saves = game:GetService("DataStoreService"):GetDataStore("Saves") --Change the name to whatever DataStore you're currently using for saving stuff
--Note: This only works for StringValues, if you want to save anything else this can be easily modded, but I will not be doing it for you
function Load() local Data = Saves:GetAsync(Folder.Name) or {} for i,v in next, Data do local Value = Instance.new("StringValue", Folder) Value.Name = i Value.Value = v end end
function Save() local Data = {} for i,v in next, Folder:GetChildren() do Data[v.Name] = v.Value end Saves:SetAsync(Folder.Name, Data) end
Load()
game.OnClose = Save
Sorry about that |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 12:22 PM |
It's fine, I'm usng his. At least it works.
|
|
|
| Report Abuse |
|
|
|
| 09 Jul 2016 12:24 PM |
| Yes but his is ridiculously overkill. Using it because you're too lazy to tell me the error so we can debug the efficient one just shows how much you care about whatever you're making. |
|
|
| Report Abuse |
|
|