How to make this tool giver script work?

I want to make this button that gives you a tool (directly inside the button inside) when you click on it. I’ve got this script, but it doesn’t work. How do I make it work? I got two errors, but they don’t relate to the tool giving script.

player = script.Parent.Parent.Parent
backpack = player.Backpack

function onClick()
	for i, v in pairs(class:GetChildren()) do
		if v:IsA("Tool") then
			v:clone().Parent = backpack



		elseif v:IsA("HopperBin") then
			v:clone().Parent = backpack
		end
	end
	
	
end
2 Likes

Thats because you never connected the function to an event. ex:

local button = --some random button or something

button.MouseButton1Down:Connect(OnClick)
1 Like

like this?

local button = script.Parent
	


player = script.Parent.Parent.Parent
backpack = player.Backpack

function button.MouseButton1Down:Connect(OnClick)
	for i, v in pairs(class:GetChildren()) do
		if v:IsA("Tool") then
			v:clone().Parent = backpack



		elseif v:IsA("HopperBin") then
			v:clone().Parent = backpack
		end
	end
	
	
end

I have no idea how you translated it to that, but no. Like this:

local button = script.Parent
local player = script.Parent.Parent.Parent
local backpack = player.Backpack

function onClick()
	for i, v in pairs(class:GetChildren()) do
		if v:IsA("Tool") then
			v:clone().Parent = backpack



		elseif v:IsA("HopperBin") then
			v:clone().Parent = backpack
		end
	end
	
	
end

button.MouseButton1Down:Connect(OnClick)

Yeah I have no clue what I’m doing, I’m just jamming code together and hope it works

Yeah, don’t do that, you should try to use roblox’s developer site there will be some useful stuff there

There are 2 main ways to connect funtions.

event:Connect(function() 
     --function body--
end)

event:Connect(OnClick())

Clone() function has a capital “C”. Copy and paste this code here:

local button = script.Parent
local player = script.Parent.Parent.Parent
local backpack = player.Backpack

function onClick()
	for i, v in pairs(class:GetChildren()) do
		if v:IsA("Tool") then
    		v:Clone().Parent = backpack



    	elseif v:IsA("HopperBin") then
    		v:Clone().Parent = backpack
		end
	end


end

button.MouseButton1Down:Connect(OnClick)