I trying to run a player join command that a tool will be put into their inventory. I am not using the StarterPack because I am trying to use DataStores. Can you help me with my code? There is no error in output when I run a server.
game.Players.PlayerAdded:Connect(function(player)
local tool = game.ReplicatedStorage.Equippables["Blue Brick"]:Clone()
tool.Parent = player.Backpack
end)
local DataService = game:GetService("DataStoreService")
local ToolsData = DataService:GetDataStore("ToolsData")
local Equipables = game.ReplicatedStorage.Equippables
game.Players.PlayerAdded:Connect(function(plr)
local toolsSaved = ToolsData:GetAsync(plr.UserId) or {}
for i, toolSaved in pairs(toolsSaved) do
if Equipables:FindFirstChild(toolSaved) then
Equipables[toolSaved]:Clone().Parent = plr:WaitForChild("Backpack")
end
end
plr.CharacterRemoving:Connect(function(char)
char.Humanoid:UnequipTools()
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsOwned = {}
for i, Tool in pairs(plr.StarterGear:GetChildren()) do
table.insert(toolsOwned, Tool.Name)
end
local success, errormsg = pcall(function()
ToolsData:SetAsync(plr.UserId, toolsOwned)
end)
if errormsg then warn(errormsg) end
end)
You can use this script to save the players backpack before leaving and when they join back, they will recieve the tool again.