Hello again guys. I made a script to save items using the database system according to the guide on YouTube, but something goes wrong and the UnquipTools value is not found or simply does not exist. Help me to understand!
UnquipTools is not a valid member of Humanoid "Workspace.MaaXGaz20.Humanoid"
local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("playersitem")
local Repstore = game:GetService("ReplicatedStorage")
local toolsFolder = Repstore:WaitForChild("Tools")
game.Players.PlayerAdded:Connect(function(plr)
local savedItems = data:GetAsync(plr.UserId.."-Items")
if savedItems then
for i, item in pairs(savedItems) do
for i, tool in pairs(toolsFolder:GetChildren()) do
if item == tool.Name then
local toolClone1 = tool:Clone()
toolClone1.Parent = plr.Backpack
local toolClone2 = tool:Clone()
toolClone2.Parent = plr.StarterGear
end
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
plr.CharacterRemoving:Connect(function(char)
local humanoid = char.Humanoid
humanoid:UnquipTools()
end)
local suc, err = pcall(function()
local savedItems = {}
for i, item in pairs(plr.Backpack:GetChildren()) do
table.insert(savedItems, item.Name)
end
data:SetAsync(plr.UserId.."-Items", savedItems)
end)
end)
Seems to be doing some odd things here … all you need to do is call the UnequipTools()
Then build the database … Locate the humanoid, call UnequipTools(), create datastore.
Looks like you’re on the right path here. Just not sure if you have all the time needed to pull this off.
Might be something you may have to do when they get a new item …
Without creating a database to check this … here is a simple rewrite to catch some obvious errors.
local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("playersitem")
local Repstore = game:GetService("ReplicatedStorage")
local toolsFolder = Repstore:WaitForChild("Tools")
game.Players.PlayerAdded:Connect(function(plr)
local savedItems = data:GetAsync(plr.UserId..Value.."-Items")
if savedItems then
for _, item in pairs(savedItems) do
for _, tool in pairs(toolsFolder:GetChildren()) do
if item == tool.Name then
local toolClone = tool:Clone()
toolClone.Parent = plr.Backpack
local toolClone2 = tool:Clone()
toolClone2.Parent = plr.StarterGear
end
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
plr.CharacterRemoving:Connect(function(char)
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
humanoid:UnequipTools()
end
end)
local savedItems = {}
for _, item in pairs(plr.Backpack:GetChildren()) do
if item:IsA("Tool") then
table.insert(savedItems, item.Name)
end
end
data:SetAsync(plr.UserId.."-Items", savedItems)
end)
local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("playersitem")
local Repstore = game:GetService("ReplicatedStorage")
local toolsFolder = Repstore:WaitForChild("Tools")
game.Players.PlayerAdded:Connect(function(plr)
local savedItems = data:GetAsync(plr.UserId .. "-Items")
if savedItems then
for _, item in pairs(savedItems) do
for _, tool in pairs(toolsFolder:GetChildren()) do
if item == tool.Name then
local toolClone = tool:Clone()
toolClone.Parent = plr.Backpack
local toolClone2 = tool:Clone()
toolClone2.Parent = plr.StarterGear
end
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
plr.CharacterRemoving:Connect(function(char)
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
humanoid:UnequipTools()
end
end)
local savedItems = {}
for _, item in pairs(plr.Backpack:GetChildren()) do
if item:IsA("Tool") then
table.insert(savedItems, item.Name)
end
end
data:SetAsync(plr.UserId .. "-Items", savedItems)
end)