so how would I use it to save my table?
First of all watch the beginner tutorial video under the thread or just try to understand the post. After youâve done this, youâll need to open the players data store then set the date storeâs value to your new data. Also, you need to destroy the players datastore when they leave. To get the data you can just use .find to get the players datastore and read its value
This is necessary to save just 3 string values? Is there a simpler way to do it? Because I keep running into errors
I just got online when I saw this but Iâll show you a simplified way of how I would do it.
I didnât have time to comment on anything so just ask if youâre confused about something
Edit: I noticed I didnât add loading the data onto the values so I added that. Also, removed unnecessary code.
local Players = game:GetService("Players")
local SDM = require(Path.To.Module)
local Template = {
Primary = "None",
Secondary = "None",
Melee = "None"
}
local function PlayerAdded(Player:Player)
local PlayerDS = SDM.new("Example", Player.UserId)
if PlayerDS:Open(Template) ~= "Success" then Player:Kick("Unable to load data!") return end
local function CreateStringValues(Name:string, Parent:Instance) : StringValue
local Uhh = Instance.new("StringValue")
Uhh.Name = Name
Uhh.Value = PlayerDS.Value[Name]
Uhh.Parent = Parent
local Connection:RBXScriptConnection;
Connection = Uhh.Changed:Connect(function(value: string)
if not PlayerDS.State then
if Connection then
Connection:Disconnect()
Connection = nil
end
return
end
PlayerDS.Value[Name] = value
end)
end
local Folder = Instance.new("Folder")
Folder.Name = "Folder"
Folder.Parent = Player
for I, X in pairs(Template) do
CreateStringValues(I, Folder)
end
end
local function PlayerRemoving(Player:Player)
local FoundDS = SDM.find("Example", Player.UserId)
if FoundDS then FoundDS:Destroy() end
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)
What is does Uhh represent and can I save it in an already existing folder instead of making a new one?
I didnât know how to name âUhhâ sorry
-
âUhhâ represents a stringValue.
-
In the loop you can pass a different parent for the stringValues created. This means you can delete the code that creates a new folder.
-
Does the code work for you?
Btw wanted to add this⌠If you change the string value in the folder during run time it will automatically save in the datatstore so you donât have to worry about manually saving data when a player leaves
AlsoâŚ
- if the variables change in the template outside of the script, it would save
- Can I use an existing string value instead of making more?
-
If you add new data then the next time you load your data table will grow. However, if you switch or delete those three keys, only the next player with no data will get the new updated data template applied as their data table. This means if you wanted to create a fresh new template with different keys then youâd have to create a new datastore or wipe everyoneâs data.
-
Can you show me your original code with your string values? Because the way i did it, the changes to the string values will automatically apply to the keys within the players Datatstore value.
I just basically change the values without data stores
script.Parent.Event.OnServerEvent:Connect(function(player)
local WeaponTypeValue = player.Character:FindFirstChild("WeaponSelection").Melee
local WeaponName = script.Parent.Text
if WeaponTypeValue.Value ~= WeaponName then
if not player.Backpack:FindFirstChild(WeaponTypeValue.Value) then
player.Character:FindFirstChild(WeaponTypeValue.Value):Destroy()
else
player.Backpack:FindFirstChild(WeaponTypeValue.Value):Destroy()
end
WeaponTypeValue.Value = script.Parent.Text -- <- This changes the value
end
end)