Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I made a script that gives an array of tools to the player but doesnt work?

This script gives the player blocks(tools) to us for building, I put the blocks in a folder and here is the script...

local function GiveBlocks()
	local getBlocks = game.ReplicatedStorage.Blocks:GetChildren()
	local getPlayers = game.Players:GetChildren()
	
	for i = 1, #getPlayers do
		for i = 1, #getBlocks do
			local BlockCopy = getBlocks[i]:Clone()
			BlockCopy.Parent = getPlayers[i].Backpack
		end
	end
end

The blocks are:

Cobblstone Grass Wooden Planks

But only the Cobblestone is given and it prints this error message:

11:38:15.966 - ServerScriptService.RoundHandler:33: attempt to index nil with 'Backpack'

1 answer

✓ Accepted answer

Great work! However, I noticed some minor issues.

Here, try this.

Variables count in for loops.

local function GiveBlocks()
    local getBlocks = game.ReplicatedStorage.Blocks:GetChildren()
    --local getPlayers = game.Players:GetChildren()
	local getPlayers = game.Players:GetPlayers()
    for i = 1, #getPlayers do
        for y = 1, #getBlocks do -- by this point you have two for loops both using variable i. just change the variable. 
            local BlockCopy = getBlocks[y]:Clone() -- we also need to change the variable to speak to the loop we want. 
            BlockCopy.Parent = getPlayers[i].Backpack
        end
    end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.