Don't know how to save tools to a specific player with datastore

Trying to modify a chest that stores all tools a player puts in it, right now all the stored tools are able to be accessed by all players but I’m trying to make it where only the person that stored the tools can acces them.

I’ve never really used datastores so any help would be very much appreciated

I’ll include both the script that I’m trying to fix and the full model.

if script.Enabled.Value.Value == true then	
local Folder = script.ToolsFolder
Folder.Parent = nil
local toolsExisting = {}	
local dss = game:GetService("DataStoreService")
local Data = ""..script.Parent.Parent.Name.."-tools"	
local toolsDS = dss:GetDataStore(Data)	
local toolsFolder = Folder
local descendants = game:GetDescendants()
for index, descendant in pairs(descendants) do
if descendant:IsA("Tool") then
local Clone = descendant:Clone()
Clone.Parent = Folder
end
end

local getSuccess, data = pcall(function()
local toolsSaved = toolsDS:GetAsync(tostring(toolsDS))
	
if toolsSaved then	
for i, toolSaved in pairs(toolsSaved) do
			
if toolsFolder:FindFirstChild(toolSaved) then 
			
toolsFolder[toolSaved]:Clone().Parent = script.Chest.Value
end			
end
else
end		
end)



	
game:BindToClose(function()

toolsExisting = {}
	
for i, toolInChest in pairs(script.Chest.Value:GetChildren()) do
		
table.insert(toolsExisting, toolInChest.Name)
end
	
local success, errormsg = pcall(function()
		
toolsDS:SetAsync(Data, toolsExisting)
end)
if errormsg then warn(errormsg) end
end)
end

Tool storage.rbxm (27.4 KB)

You are using the DataStores wrong. First of all, if this is in a local script, it won’t work. DataStores only work in Server scripts. Second of all, you don’t want to make a new datastore for every player like what you are doing. Instead, make a general datastore for all player’s tool saves and get each players data individually like this:

local dss = game:GetService("DataStoreService")
local toolsDS = DSS:GetDataStore("Tools")
local playerData = toolsDS:GetAsync(player.UserId.."-tools")

It is also better practice to save a player’s data with their UserId as I did above instead of their name in case they change their roblox username. There are also many tutorials for datastores on youtube, and they are very helpful so you should check some of those out too.

1 Like

Thanks alot, I didn’t realise I was making a new datastore everytime. I think that also explains why the tools inside would get reset after making changes to the script.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.