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

[SOLVED] How come my custom Tool Equipping system keeps sending the same error?

Asked by 1 year ago
Edited by imKirda 1 year ago

For the past 5 hours I've been working on a Tool Equipping system, but for some reason I keep being sent the exact same Error. I think I've located the the source to the issue, but when ever I attempt to fix it never works.

The script is located in the StaterPlayerScripts.
The script only starts when an event called "SpawnEvent"

--Error

is not a valid member of Backpack "Backpack" - Client - LocalScript:66

local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer

local Events = ReplicatedStorage:WaitForChild("Events")

local SpawnEvent = Events:WaitForChild("SpawnEvent")

local PlrBackPack = Player:WaitForChild("Backpack")

local Weapons = {

    Primary = "",

    Secondary = "",

    Melee = ""

}

local HasPrimary = false
local HasSecondary = false
local HasMelee = false

--------------------Tool Equipping 
local function SetUp()
    print(PlrBackPack:GetChildren())

    for i, Tool in pairs(PlrBackPack:GetChildren()) do --Empty even though it shows and I know it isn't empty.
        if Tool:isA("Tool") then
            if Tool:GetAttribute("WeaponType") == "Primary" then
                Weapons.Primary = Tool.Name
                print(Weapons.Primary.Name)
            elseif Tool:GetAttribute("WeaponType") == "Secondary" then
                Weapons.Secondary = Tool.Name
                print(Weapons.Secondary.Name)
            elseif Tool:GetAttribute("WeaponType") == "Melee" then
                Weapons.Melee = Tool.Name
                print(Weapons.Melee.Name)
            end
        end
    end

end

local function OnInput(input, Chat) 
    local Character = Player.Character
    local Humanoid = Character.Humanoid

    if Chat then return end

    local GetKeyCode = input.KeyCode
    if GetKeyCode == Enum.KeyCode.One then
        if HasPrimary == false then
            Humanoid:EquipTool(PlrBackPack[Weapons.Primary])
            HasPrimary = true
            HasSecondary = false
            HasMelee = false
        end
    elseif GetKeyCode == Enum.KeyCode.Two then
        if HasSecondary == false then
            Humanoid:EquipTool(PlrBackPack[Weapons.Secondary])
            HasPrimary = false
            HasSecondary= true
            HasMelee = false
        end
    elseif GetKeyCode == Enum.KeyCode.Three then
        if HasMelee == false then
            Humanoid:EquipTool(PlrBackPack[Weapons.Melee])
            HasPrimary = false
            HasSecondary = false
            HasMelee = true
        end
    end

end

UserInputService.InputBegan:Connect(OnInput)

SpawnEvent.OnClientEvent:Connect(function()
    SetUp()

    wait(.1)

    OnInput({KeyCode = Enum.KeyCode.One})-- initializes the force tool equipping
end)

0
on line 34 print(PlrBackPack:GetChildren()), the table is empty right? imKirda 4491 — 1y
0
No it shouldn't be as it represents the players backpack, and in game that has tools in it. vincentthecat1 199 — 1y
0
I just did a test. For some reason its completely empty when it shouldn't be. vincentthecat1 199 — 1y
0
a copy of backpack is made after you respawn or something, try putting local PlrBackPack = Player:WaitForChild("Backpack") inside the SetUp function imKirda 4491 — 1y
View all comments (3 more)
0
I'll try it vincentthecat1 199 — 1y
0
It worked. vincentthecat1 199 — 1y
0
Such a simple solution, but I never knew the backpack was replicated. vincentthecat1 199 — 1y

Answer this question