Hi, I’m trying to make a shop that saves the tools you’ve already bought by putting whatever you’ve purchased inside of a folder called “Tools” that is inside of the player. I want to save what tools are in this folder when the player leaves, and load them when the player rejoins. But when it saves the data I keep getting an error that says “attempt to call a nil value.” I’m very new to datastores so I’ve tried everything to fix it that is of my knowledge, if somebody could help me out that would be greatly appreciated!
Code:
local DataStoreService = game:GetService("DataStoreService")
local ToolsSave = DataStoreService:GetDataStore("ToolsSave")
game.Players.PlayerAdded:Connect(function(player)
local Tools = Instance.new("Folder",player)
Tools.Name = "Tools"
local success, errormessage = pcall(function()
local Tool = ToolsSave:GetAsync("-Tools",player.UserId)
if Tool then
for i,v in pairs(Tool) do
local ToolFound = game.ServerStorage.Tools:FindFirstChild(v)
if ToolFound then
ToolFound:Clone().Parent = player.Backpack
ToolFound:Clone().Parent = player.StarterGear
ToolFound:Clone().Parent = player.Tools
end
end
end
end)
if success then
print("Worked")
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
local ToolsSave = {}
for i,tool in pairs(player.Tools:GetChildren()) do
if tool then
table.insert(ToolsSave,tool.Name)
print(ToolsSave[1])
end
end
-- This line is where the error is
ToolsSave:SetAsync("-Tools"..player.UserId,ToolsSave)
end)
if success then
print("Worked")
else
warn(errormessage)
end
end)