I have a lot of tools in ReplicatedStorage, and want to clone and put them in the players backpack, but I don’t want to copy all of the tools into the player’s backpack. I tried putting them in a folder using GetChildren() but when trying to clone a GetChildren variable the output says:
Workspace.Plot1Build.Script:4: attempt to call a nil value
Any way to copy multiple tools without having to write variables for them individually?
local FixedTools = game.ReplicatedStorage.FixedTools:GetChildren()
script.Parent.Touched:Connect(function(hit, player)
if hit.Parent:FindFirstChild("Humanoid") then
for _, child in pairs(FixedTools) do
local FixedClone = FixedTools:Clone()
FixedClone.Parent = player.Backpack
end
end
end)
I changed the code a little, now I get this error:
new code:
local FixedTools = game.ReplicatedStorage.FixedTools:GetChildren()
script.Parent.Touched:Connect(function(hit, player)
if hit.Parent:FindFirstChild("Humanoid") then
for _, child in pairs(FixedTools) do
if child:IsA("Tool") then
local childclone = child:Clone()
childclone.Parent = player.Backpack
end
end
end
end)
There’s a possibility that hit.Parent is not getting the player or something along the lines of that
You can get the player using :GetPlayerFromCharacter
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)