Selection GUI not working

I have a gui that should get the name of a button when it is pressed, so it can find that weapon in serverstorage and clone it to the player’s backpack. For some reason, it gets nil as the value for the button.

The script:

local player = script.Parent.Parent.Parent.Parent
local backpack = player.Backpack
local serverStorage = game:GetService("ServerStorage")

for _, button in pairs(script.Parent.Main:GetChildren()) do
	if button:IsA('TextButton') then
		button.MouseButton1Click:Connect(function(button)
			print(button.Name)
			local getclass = serverStorage:WaitForChild(button.Name)
			if getclass:IsA("Tool") then
				script.Parent.Visible = false
				getclass:clone().Parent = backpack
				local getutil1 = serverStorage.UtilityWeapons:WaitForChild(script.Parent.Utility1.Value)
				getutil1:clone().Parent = backpack
				local getutil2 = serverStorage.UtilityWeapons:WaitForChild(script.Parent.Utility2.Value)
				getutil2:clone().Parent = backpack
				local getutil3 = serverStorage.UtilityWeapons:WaitForChild(script.Parent.Utility3.Value)
				getutil3:clone().Parent = backpack
				local getutil4 = serverStorage.UtilityWeapons:WaitForChild(script.Parent.Utility4.Value)
				getutil4:clone().Parent = backpack
			end
		end)
	end
end

function onHumanoidDied(humanoid, player)
	script.Parent.Visible = true
end

Explorer:
image

Any help would be much appreciated!

Clients (local scripts) can not access ServerStorage! You will need to use RemoteEvents for this. You could move the tools into ReplicatedStorage, this would allow the client to see the tools, but cloning them to their backpack won’t show up for other players (I think at least. Unsure about this since the client controls the humanoid.).

It is not a local script. All of this is handled inside a server script. The problem is that the name of the button will return as nil.

My bad. I missed that image. I would recommend not using regular scripts in GUI though.

I think the issue is this:

You are receiving the variable button, while you already have a variable called button. This is bound to cause some issues.

Renaming the variable still causes the button to return as nil.

Which button are we talking about now? The button used for .MouseButton1Click is not nil. The other button will always be null, as GuiButton.MouseButton1Click (roblox.com) doesn’t return any values.

Also, it looks like the desired value returned from button.MouseButton1Click would be the button which caused it. Simply using that button should do the trick.

You are correct, trying to call the variable in the function would cause the button to be overriden with nothing. This was a silly mistake on my part. Thanks for the help!

No worries! Happens to the best of us.