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

Why won't the player's backpack autoequip by clicking a gui?

I'm trying to make the player, whenever he/she clicks a gui button, equip a tool from the player's backpack. It says it keeps failing at line 15. The error is listed below. Can someone explain to me why it is giving me an error? (Yes, it is in a LocalScript.)

Error: Players.Player1.PlayerGui.Backpack.ScrollingFrame.AutoEquip:15: attempt to index global 'findval' (a nil value)

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

local player = game.Players.LocalPlayer
local character = player.Character
local tool = player.Backpack:GetChildren()

val = script.Parent.EquipedItem.Value
findval = player.Backpack:FindFirstChild(val)

function equip(tool)
    findval:Clone().Parent = character
end

function unequip(tool)
    findval:Destroy()
end

while wait() do
	if val == "" then
		unequip()
	elseif val == "Sword" then
		equip()
	end
end

1 answer

✓ Accepted answer

An easier way to do this would be how I've done it below assuming the LocalScript is inside the button of the Gui.

TOOLHERE = game.Players.LocalPlayer.Backpack["SwordOrSomething"]

script.Parent.MouseButton1Down:connect(function()
	game.Players.LocalPlayer.Character.Humanoid:EquipTool(TOOLHERE)
end)

to un-equip the tools you might as well use this line it's just nice and simple. I'm not saying this is the best way to do it I'm just giving you an easier option to what you have already.

game.Players.LocalPlayer.Character.Humanoid:UnequipTools()

There are plenty more people on here that can explain this better than me.

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