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

How would I save gears (player backpack) by datastore?

I've been trying to figure out how to do this. I just want a player's gear (backpack) to autosave so the next time they play they will have those items. Any ideas?

1 comment

0

There are a lot of methods but I don't know what tools you are saveing, each way you need to convert the tools into a saveable format e.g. id or string then convert this back into the tool the player had.

1 answer

✓ Accepted answer

You need to use a table and a Data Store .

Let's take an example. Our player is Bob.

Our stuff will be into the ReplicatedStorage.

When Bob leave the game, we want to save his inventory. Note that DataStore Service doesn't save Instances.

game.Players.PlayerRemoving:connect(function(player)
	for _, Tool in pairs (player.BackPack:GetChildren()) do
		-- Stuff
	end
end)

When Bob leaves the game, he fires a function called PlayerRemoving and he'll be called "player". We want to know what he has in his inventory, so we use the for function. It will get Bob's BackPack's Stuff into a table and repeat something for each things of the table we make with the :GetChildren() function.

Now we need to* check if the thing is a Tool*, and if yes, add it to a new table so when Bob is joining the game, we can load the stuff.

game.Players.PlayerRemoving:connect(function(player)

	local Backpack = {}

	for _, Tool in pairs (player.BackPack:GetChildren()) do
		if Tool:IsA("Tool") then
			table.insert(Backpack, tostring(Tool))
		end
	end
end)

Now, we have a table called Backpack.

Each time a tool is detected, its name will be inserted into the Backpack table.


local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService"):GetDataStore("Backpack")


game.Players.PlayerRemoving:connect(function(player)

	local Backpack = {}

	for _, Tool in pairs (player.BackPack:GetChildren()) do
		if Tool:IsA("Tool") then
			table.insert(Backpack, tostring(Tool))
		end
	end

	local BackpackJSON = HttpService:EncodeJSON(Backpack)
	DataStoreService:SetAsync("user_"..tostring(player.UserId), BackpackJSON)
end)

game.Players.PlayerAdded:connect(function(player)
	if DataStoreService:GetAsync("user_"..tostring(player.UserId), Stuff) then
		for _, NewTool in pairs (HttpService:DecodeJSON(DataStoreService:GetAsync("user_"..tostring(player.UserId))) do
			local Tool = game.ReplicatedStorage:FindFirstChild(tostring(NewTool))
			if Tool == nil then return end
			local ClonedTool = Tool:Clone()
			ClonedTool.Parent = player.BackPack
			print("Added "..tostring(NewTool))
		end

	else
		warn(tostring(player).." doesn't have save")
	end

end)

Here is all the script. After inserted all the tools' name into the table, we encode the table in JSON using HttpService so everybody can understand the table. Then we save the JSON table into the DataStore Service with a key. The key is "user_" + the player's id. Mine is 25776731, so it could be "user_25776731" for me.

When a player joins, we check if the player has a save, if not then it stops. If yes, then we get the table we made using :GetAsync with the key of the player. Then we decode it. If the tool's name is inside the ReplicatedStorage, then it clones the tool, and add it to the player's backpack. Each time of each tool in the table

Hope it will work for you!!!!! (Really long)

Don't forget to accept my answer if it works!

7 comments

0

I tried this but it did not work. I copied the 33 lines of code into a default type script and put it into replicated storage. When I look at the script in line 16 the "=" between Stuff and BackpackJson has the little red squiggle line under it, so I assume that is the problem. Do you know how to fix this?

0

You must put the script into the Workspace or ServerScriptService and the tools into ReplicatedStorage. I made an error in the script, I'll correct it!

0

Tell me if doesn't work again.

0

yeh it doesnt work again, line 20 'stuff' has a squiggle' and i think a bracket was missing somewhere but i fixed that

0

Also Spelling error: Its 'Backpack' Not 'BackPack' :)

0

Ok thanks for the comment!

0

Problem21: ')' expected near 'do'

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