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 a tool to a player?

Im not asking for the script but is there a way to do it?

Like when a player gets a tool to his/her inventory it saves. I dont know how to use data store to tools and dont understand how to save tools in a table.

2 comments

0

Do you want it to save on the server or over different server instances?

0

server

2 answers

✓ Accepted answer

If you want to save tools to a table, the best way to do it is convert it to a string, most likely its name and put that string in the table. When you want to load them back, just iterate using a for loop and FindFirstChild() to create a clone of the tool and put it into their inventory.

Saving Code:

function SaveTools(Player)
	inventory={}
	for i,v pairs(player.Backpack:GetChildren()) do
		table.insert(inventory,v.Name)
	end
	return inventory
end

Calling this function with a player object will return the table with all their tools. This can then be saved any way you want.

Loading Code:

function LoadTools(Player,Inventory)
	Player.Backpack:ClearAllChildren()
	for i,v in pairs(Inventory) do
		c=toolfolder:FindFirstChild(v):Clone()
		c.Parent=Player.Backpack
	end
end

Calling this code with a player object and the table generated by the SaveTools function above will give them all their tools back

You're going to want to use RemoteEvents and FilteringEnabled for this as you don't want someone to exploit and add tools locally that will then be saved.

In your LocalScript you simply want to wait for the script to create a RemoteEvent and wait for your backpack to load. With your LocalScript you fire the RemoteEvent, which will then load and set saved data as well as handle all future functions.

Your script will then do all the heavy lifting. Set an empty table in your script. When your script loads your data, set your empty table to your loaded table of tools, so you don't have to use GetAsync every time on the next part. Every time something is added, check your table to see if you have it saved already. If it isn't saved, add a new index to the table with the tools name and use SetAsync to update your old saved table with the table in your script. Remember to wrap all DataStore functions in a pcall().

You can save tools in a table dictionary

local saved_tools = {}

if saved_tools[tool.Name] then 
	print("Tool already saved")
else
	saved_tools[tool.Name] = true
	-- SetAsync to replace your old table with saved_tools (aka the updated table)
end

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