I would like to find out how to access the backpack in a script in ServerScriptService.
Its printing in output “player.Backpack or player.Character is nil.”
game.Players.PlayerAdded:Connect(function(player)
local items = game.ReplicatedStorage.GearsShopTools
for _, boughtItemsFolder in pairs(player:WaitForChild("BoughtItems"):GetChildren()) do
local itemName = boughtItemsFolder.Name
local itemToClone = items:FindFirstChild(itemName)
if itemToClone then
print("FOUND")
if player.Backpack and player.Character then
if not player.Backpack:FindFirstChild(itemName) and not player.Character:FindFirstChild(itemName) then
print("gave items")
local itemClone = itemToClone:Clone()
itemClone.Parent = player.Backpack
end
else
print("player.Backpack or player.Character is nil.")
end
end
end
end)
Try Letting it Yield a bit to load? Something like…
game.Players.PlayerAdded:Connect(function(player)
repeat task.wait() until game.Workspace:FindFirstChild(player.Name) ~= nil
local items = game.ReplicatedStorage.GearsShopTools
for _, boughtItemsFolder in pairs(player:WaitForChild("BoughtItems"):GetChildren()) do
local itemName = boughtItemsFolder.Name
local itemToClone = items:FindFirstChild(itemName)
if itemToClone then
print("FOUND")
if player.Backpack and player.Character then
if not player.Backpack:FindFirstChild(itemName) and not player.Character:FindFirstChild(itemName) then
print("gave items")
local itemClone = itemToClone:Clone()
itemClone.Parent = player.Backpack
end
else
print("player.Backpack or player.Character is nil.")
end
end
end
end)