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

Tools equipping start to act weird in GUI?

Asked by
fr2013 88
5 years ago

So when I try to equip an item, it equips it normally and works. But when I select another item, it should make the text become "Equip" even though it stays at "Unequip". And once you select another item and press the Equip Button that says "Unequip", it'll make the tool disappear and make the new tool equip to the player. Does the same thing to other tools. They're all located in the "Swords" folder in my Backpack. But it seems to be that the item that disappears goes into the Backpack folder instead of the "Swords" folder.

https://gyazo.com/5f43e9d99e8162689f161efd686fbd6c https://gyazo.com/32ecbf264a7bac1851ef1a5d07d5757d

Equip Script:

local player = game.Players.LocalPlayer
local character = player.Character
local items = {}
local buttons = {}
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false) -- Makes the original backpack gui invisible

function search(location)
    for i,v in pairs(location:GetChildren()) do -- Find all item in a specific location
        if v:isA("Tool") then -- If the item found is a "Tool"
            table.insert(items,v) -- Going to put all the tools found in a table.
        end
    end
end

function refresh()
    for i,v in pairs(buttons) do -- Finds all items in the table
        v:Destroy() -- Destroy 'em all
    end
    for i,v in pairs(items) do -- Finds all items in the table
        local button = script.Sample:Clone() -- clones the sample button inside the localscript
        button.Name = v.Name -- sets the cloned button's name to the name of the item
        button.LayoutOrder = i
        button.Parent = script.Parent.HandlerS -- Sets the parent of the cloned button to the HandlerS
        button.Image = v.TextureId -- Sets the image of the button to the texture id of the tool
        table.insert(buttons,button) -- Inserts the button to our table "buttons"
        button.MouseButton1Click:connect(function()
            if script.Parent.HandlerS.Selected.Value == nil or script.Parent.HandlerS.Selected.Value ~= v then -- Checks if the selected value is nothing or if the selected value is not the button
                script.Parent.Frame.ItemName.Text = v.Name -- Sets the TextLabel's Text to the name of the tool/button
                script.Parent.Frame.ItemInfo.Text = v.ToolTip -- Sets the TextInfo's Text to the Info of the tool/button
                script.Parent.Frame.ImageLabel.Image = v.TextureId -- Sets the image label's image to the texture id of the tool
                script.Parent.HandlerS.Selected.Value = v
                if script.Parent.HandlerS.Selected.Value ~= script.Parent.HandlerS.Equipped.Value then --if the selected value is not the same as the equipped value then
                    script.Parent.HandlerS.Location.Value = v.Parent -- Sets the value of our location to the parent of the tool whether it is in the backpack or in the character
                    script.Parent.Frame.Equip.Text = "Equip"
                elseif script.Parent.HandlerS.Selected.Value == script.Parent.HandlerS.Equipped.Value then -- If the selected value is the same as the equipped value then...
                    script.Parent.HandlerS.Location.Value = v.Parent
                    script.Parent.Frame.Equip.Text = "Unequip"
                end
            end
        end)
    end
end

function backpackRefresh()
    items = {}
    search(character)
    search(player.Backpack:WaitForChild("Swords"))
    refresh()
end

backpackRefresh()

player.Backpack:WaitForChild("Swords").ChildAdded:connect(backpackRefresh)
player.Backpack:WaitForChild("Swords").ChildRemoved:connect(backpackRefresh)

character.ChildAdded:connect(backpackRefresh)
character.ChildRemoved:connect(backpackRefresh)

Equip Button:

local equipped = script.Parent.Parent.Parent.HandlerS.Equipped
local selected = script.Parent.Parent.Parent.HandlerS.Selected
local location = script.Parent.Parent.Parent.HandlerS.Location
local player = game.Players.LocalPlayer
local character = player.Character

script.Parent.MouseButton1Click:connect(function()
    if equipped.Value == nil or equipped.Value ~= selected.Value then
        character.Humanoid:UnequipTools() -- Forces the player to unequip the tool that they equipped
        if location.Value == player.Backpack:WaitForChild("Swords") then
            character.Humanoid:EquipTool(selected.Value)
            equipped.Value = selected.Value
            script.Parent.Text = "Unequip"
        end
    else
        local children = player.Character:GetChildren()
        for i = 1, table.getn(children), 1 do
            if children[i].ClassName == "Tool" then
                children[i].Parent = player.Backpack:WaitForChild("Swords")
            end
        end
        equipped.Value = nil
        script.Parent.Text = "Equip"
    end
end)

Answer this question