So I am currently creating three tabs of inventory, which are swords, potions, and magic. They are all made in Backpack. My Backpack GUI does register them each and they are also cloned in a specific folder when I buy them from the shop. So when I die, it'll save the tools and there place inside. But it duplicates the folder (2x) one with the tools and the other which is empty, while the tools duplicate (2x) inside of the folder that has tools. And also it doesn't save the tools when I leave and then rejoin the game too.
local InStudio = game:GetService("RunService"):IsStudio()
if not InStudio then
sword = game:GetService("DataStoreService"):GetDataStore("Swords")
potion = game:GetService("DataStoreService"):GetDataStore("Potions")
magic = game:GetService("DataStoreService"):GetDataStore("Magic")
end
function savedata(dataname, playerid, value)
if InStudio then return end
dataname:SetAsync(playerid, value)
end
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(char)
local data1 = nil
local data2 = nil
local data3 = nil
data1 = sword:GetAsync(player.UserId)
data2 = potion:GetAsync(player.UserId)
data3 = magic:GetAsync(player.UserId)
local sword = Instance.new("Folder")
sword.Name = "Swords"
sword.Parent = player:WaitForChild("Backpack")
local potion = Instance.new("Folder")
potion.Name = "Potions"
potion.Parent = player:WaitForChild("Backpack")
local magic = Instance.new("Folder")
magic.Name = "Magic"
magic.Parent = player:WaitForChild("Backpack")
local sword = Instance.new("Folder")
sword.Name = "Swords"
sword.Parent = player:WaitForChild("StarterGear")
local potion = Instance.new("Folder")
potion.Name = "Potions"
potion.Parent = player:WaitForChild("StarterGear")
local magic = Instance.new("Folder")
magic.Name = "Magic"
magic.Parent = player:WaitForChild("StarterGear")
char.Humanoid.Died:connect(function()
char.Humanoid:UnequipTools()
local old = player.StarterGear:GetChildren()
for i = 1, #old do
old[i]:Destroy()
end
local new = player.Backpack:GetChildren()
for i = 1, #new do
new[i].Parent = player.StarterGear
end
end)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
savedata(sword, player.userId, player.Backpack:WaitForChild("Swords"))
savedata(potion, player.userId, player.Backpack:WaitForChild("Potions"))
savedata(magic, player.userId, player.Backpack:WaitForChild("Magic"))
end)

19 comments
You never call getAsync so you arent gonna pull any saved data. As i mentioned on your last post i believe the duplicate folder issue is to do with you calling it on character added.
So do I put like a data = sword:GetAsync(player.UserId) in player.CharacterAdded:Connect(function(char) ?
whats data? i dont see where you've defined that anywhere
Ok I edited it
use locals, and name them seperately. local data1 = ... local data2 = ... we cant save them into 1 variable, currently youre just changing what it holds 3 times
Ok so I re-edited it and tested out in my game, but it isn't copied in the Swords folder, just in the Backpack, even though it saves and doesn't duplicate
So I create new folders in StarterGear?
You dont need to nil them, just local data 1 = yada yada yada. Then player.sword = data1. See if that makes stuff work.
Im not a huge fan of the way you've set things up. I creates bools for any obtainable objects then true/false if owned or not. if true then on connect theyre added to player. Could do something similar with potions, just use int value to determine how many then add them.
Re-edit and I don't think it still works ;-;
I didnt mean to literally put player.sword, make that the location of your swords folder.
So I re-edited it again and it still has the same effect
Yeah i figured. I dont think datastores can work the way you're trying to do it. You'll probably have to save them item by item.
@DinozCreates Well I re-edited it and I solved the problem of duplicating items by creating a folder in StarterGear, now how do I save them when the player leaves and then rejoins?
create values for each item, add them all into tables, save the values from the tables into your data stores, save the table data into the item values.
So is it like this?: savedata(sword, player.userId, player.Backpack:WaitForChild("Swords")) and savedata(sword, player.userId, player.StarterGear:WaitForChild("Swords")) and sword:GetAsync(player.UserId) or are you actually talking about making individually all my items save? Like the swords and potions?
option #2
Dude your function is not cool hence i wont help you
Dude you never called GetAsync hence ur not getting what you stored