Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
thePyxi 179
7 years ago

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

Log in to vote
0
Answered by
Cuvette 246 Moderation Voter
7 years ago
Edited 7 years ago

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.

Ad

Answer this question