Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I datastore a whole inventory?

I have a script I use for datastoring, but thats mainly for leaderboards. If I want to datastore a whole inventory of things, how could I do this? Here is my script:

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("GunsaveSystem")


game.Players.PlayerAdded:connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "GunsFolder"
 local Gun = Instance.new("IntValue", folder)
 Gun.Name = "Gun"

 
 Gun.Value = ds1:GetAsync(plr.UserId) or 0
 ds1:SetAsync(plr.UserId, Gun.Value)
 

 Gun.Changed:connect(function()
  ds1:SetAsync(plr.UserId, Gun.Value)
 end)
 

end)

3 comments

1

Maybe if you researched this before asking, you would have found the millions of other people who asked the EXACT SAME QUESTION.

0

This is the excact script from a youtube video, did you even TRY to make it work on your own?

0

Yeah

1 answer

Idk if there's a better way to do this, but, if you can only have one of each item(i assume this is what you want since i can see it's guns) you would have a DataStore for each item, then the value gets saved as 1 or 0 depending on if you have the gun or not

local datastore = game:GetService("DataStoreService")
local RS = game:GetService("ReplicatedStorage")
local gun1 = datastore:GetDataStore("Gun1SaveSystem")
local gun2 = datastore:GetDataStore("Gun2SaveSystem")
local gun3 = datastore:GetDataStore("Gun3SaveSystem")


game.Players.PlayerAdded:connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "GunsFolder"
 local gun1 = Instance.new("IntValue", folder)
 gun1.Name = "Gun1"
 local gun2 = Instance.new("IntValue", folder)
 gun2.Name = "Gun2"
 local gun3 = Instance.new("IntValue", folder)
 gun3.Name = "Gun3"

 

 --Loading the guns
 gun1.Value = ds1:GetAsync(plr.UserId) or 0
 gun1:SetAsync(plr.UserId, gun1.Value)

 gun2.Value = ds1:GetAsync(plr.UserId) or 0
 gun2:SetAsync(plr.UserId, gun2.Value)

 gun3.Value = ds1:GetAsync(plr.UserId) or 0
 gun3:SetAsync(plr.UserId, gun3.Value)

 --Giving the guns
 if gun1.Value == 1 then
  local gun = RS.Guns.gun1:Clone()
  gun.Parent = plr.StarterGear
 end

 if gun2.Value == 1 then
  local gun = RS.Guns.gun2:Clone()
  gun.Parent = plr.StarterGear
 end

 if gun3.Value == 1 then
  local gun = RS.Guns.gun3:Clone()
  gun.Parent = plr.StarterGear
 end


  --Saving the guns
 gun1.Changed:connect(function()
  gun1:SetAsync(plr.UserId, gun1.Value)
 end)

 gun2.Changed:connect(function()
  gun2:SetAsync(plr.UserId, gun2.Value)
 end)

 gun3.Changed:connect(function()
  gun3:SetAsync(plr.UserId, gun3.Value)
 end)
end)

NOTE: I Havn't tested it, but i would do something like this

3 comments

0

Using :SetAsync() in a Changed event could cross data store limits.You should also try using a more secure method of loading existing data. See pcall() for that. You shouldn't be saving to multiple DataStores like this either.

0

this isnt very effective.

0

I did say that "Idk if there's a better way" I just tried to help with the knowledge I have

This discussion is preserved as it was. New answers, comments, and votes are closed.