-
What do you want to achieve? Keep it simple and clear!
I want to make a script that checks if a value has been added into a folder and if so then save the name of it into a table or something, and then when the player joins the game again it creates a value into the folder and the name of the value is the saved name in the table. -
What is the issue? Include screenshots / videos if possible!
I’m not good with tables. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried YT and DevForum.
Not exactly sure what you problem is since you don’t share any screenshots and excerpts from your script. If you’re saving items that are added into a folder you could create a table and every child thats added gets added to it. The table can be a dictionary where item1 = item1 info… etc. If you want to link it to a player then do this in a local script and save it in a data store. (Sorry small responce gtg)
yes, thats exactly what i want to do, but i don’t know how to save stuff in tables or dictionarys, so could you provide me with an example on how to save an item in a table and load it again?
hey um, I made a script and found some parts online, it works. But it only saves when I leave the game, how can I make it load the values names in when the player joins?
local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Tools")
game.Players.PlayerAdded:Connect(function(player)
local ownedTools = Instance.new("Folder")
ownedTools.Parent = player
ownedTools.Name = "OwnedTools"
end)
game.Players.PlayerRemoving:Connect(function(Player)
local suc, err = pcall(function()
local tab = {}
for i,tool in pairs(Player:WaitForChild("OwnedTools"):GetDescendants()) do
table.insert(tab,{tool.Name})
print(tool.Name)
end
if not datastore:GetAsync("key") then
datastore:SetAsync("key",tab)
else
datastore:UpdateAsync("key",function(oldVal)
return tab
end)
end
end)
end)
From what I understand your looking for the Instance.ChildAdded event
The event will fire when a child is added to the instance. You can use it like this:
local folder = --REFERENCE YOUR FOLDER HERE
folder.ChildAdded:Connect(function(child)
if (child.ClassName === "StringValue") then
--DO SOMETHING
print("Value Added")
end
end)
I understand that bit, but not the rest, can you help me with my script? I made it save, but I need it to load in the saved name
this is the script:
local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Tools")
game.Players.PlayerAdded:Connect(function(player)
local ownedTools = Instance.new("Folder")
ownedTools.Parent = player
ownedTools.Name = "OwnedTools"
end)
game.Players.PlayerRemoving:Connect(function(Player)
local suc, err = pcall(function()
local tab = {}
for i,tool in pairs(Player:WaitForChild("OwnedTools"):GetDescendants()) do
table.insert(tab,{tool.Name})
print(tool.Name)
end
if not datastore:GetAsync("key") then
datastore:SetAsync("key",tab)
else
datastore:UpdateAsync("key",function(oldVal)
return tab
end)
end
end)
end)
I think the problem you’re experiencing is that you saving it under the datestore “key”, I would recommend saving it as the player’s userid. another problem could be that game isn’t public, for data to save the game needs to public or else it wont work. For loading when a player joins you would do
game.Players.PlayerAdded:Connect(function(player)
local ownedTools = Instance.new("Folder")
ownedTools.Parent = player
ownedTools.Name = "OwnedTools"
local data = datastore:GetAsync(player.UserId)
if data then
for _, i in data do
local tool = Instance.new("StringValue", ownedTools); tool.Name = i
end
end
end)
ok, what do i do with the rest of the script? The player removing bit
keep it the way it is, it looks like it should work to me, except change “key” to Player.UserID
oh, it doesnt work if i keep it the same and i get no errors, this is the final script:
local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("Tools")
local tab = {}
game.Players.PlayerAdded:Connect(function(player)
local ownedTools = Instance.new("Folder")
ownedTools.Parent = player
ownedTools.Name = "OwnedTools"
local data = datastore:GetAsync(player.UserId)
if data then
for _, i in data do
local tool = Instance.new("StringValue", ownedTools); tool.Name = i
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local suc, err = pcall(function()
for i,tool in pairs(Player:WaitForChild("OwnedTools"):GetDescendants()) do
table.insert(tab,{tool.Name})
print(tool.Name)
end
if not datastore:GetAsync("key") then
datastore:SetAsync("key",tab)
else
datastore:UpdateAsync("key",function(oldVal)
return tab
end)
end
end)
end)
i forgot to tell you to change the “key” in the player removing to Player.UserID, Using the userid is the main way to make datastores work with multiple people, otherwise all players would have the same save data.
i got an error its this : Unable to assign property Name. string expected, got table
oh, it because of the brackets around tool.Name in the removing function
Hello man! I recently found a great video on how to learn data saving. It isn’t hard so I recommend you trying it out!! Here, take a look: https://youtu.be/DkYupSBUpes
Good luck!!
OMG thanks dude, this will help so much it worked
oh thanks, its okay, I found a solution for it
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.