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

Index a nil value when unequipping tool?

Asked by 4 years ago
Edited 4 years ago

10:44:37.469 - Players.AnnaVonWachstein.Backpack.Create.LocalScript:36: attempt to index a nil value 10:44:37.470 - Stack Begin 10:44:37.470 - Script 'Players.AnnaVonWachstein.Backpack.Create.LocalScript', Line 36 - upvalue GenerateSelectionModel 10:44:37.471 - Script 'Players.AnnaVonWachstein.Backpack.Create.LocalScript', Line 76 10:44:37.471 - Stack End 10:44:37.472 - RunService:fireRenderStepEarlyFunctions unexpected error while invoking callback: Players.AnnaVonWachstein.Backpack.Create.LocalScript:36: attempt to index a nil value

That's the full error; not really sure how I can work around with this.

local Player          = game.Players.LocalPlayer
wait(0.5)
local GUI             = Player.PlayerGui:FindFirstChild("MainUI")
local NavyUI          = Player.PlayerGui:FindFirstChild("MaritimeGUI")
local InfantryUI      = Player.PlayerGui:FindFirstChild("TroopsGUI")
local BuildingUI      = Player.PlayerGui:FindFirstChild("BuildingsGUI")
local Tool            = script.Parent
--Variables for Placement
local CurrentRotation = 0
local Mouse           = Player:GetMouse()
local RunService      = game:GetService("RunService")
local ContextService  = game:GetService("ContextActionService")
local SelectionModel
local Settings        = require(game:GetService("ReplicatedStorage").Settings)
local ObjectsFolder   = Settings.ObjectFolder
local Qdown, Edown
local db1             = false
local Equipped

local function CloseUIs() 
    GUI.Enabled = false
    BuildingUI.Enabled = false
    NavyUI.Enabled = false
    InfantryUI.Enabled = false
end

Tool.Equipped:Connect(function()
    GUI.Enabled = true
end)

-- Initiator 

local function GenerateSelectionModel()
    if SelectionModel then
        SelectionModel = SelectionModel:Clone()
        local CFrame1 = CFrame.new(Mouse.Hit.p) * CFrame.Angles(0, math.rad(CurrentRotation), 0) * CFrame.new(SelectionModel:FindFirstChild("Compensation").Value)
        local CFrame2 = CFrame.new(0, SelectionModel.PrimaryPart.Size.Y/2, 0)   
        SelectionModel:SetPrimaryPartCFrame(CFrame1 or CFrame2)
        local Cache = SelectionModel:GetDescendants()
        Mouse.TargetFilter = SelectionModel
            --[[
        for i = 1, #Cache do
            if Cache[i]:IsA("BasePart") then
                Cache[i].Material = Enum.Material.Glass
            end
        end ]]
        if Equipped then
            SelectionModel.Parent = workspace
        end
    end
end

local function CanPlace(PPP)
    return true
end

local function FrameRendered()
    if SelectionModel then
        if SelectionModel.PrimaryPart then
            local CFrame1 = CFrame.new(Mouse.Hit.p) * CFrame.Angles(0, math.rad(CurrentRotation), 0) * CFrame.new(SelectionModel:FindFirstChild("Compensation").Value)
            local CFrame2 = CFrame.new(0, SelectionModel.PrimaryPart.Size.Y/2, 0)
            SelectionModel:SetPrimaryPartCFrame(CFrame1 or CFrame2)
            local ReleventColor
            if CanPlace(SelectionModel.PrimaryPart.Position) then
                ReleventColor = Settings.PlaceableColor
            else
                ReleventColor = Settings.CannotPlaceColor
            end
            local Cache = SelectionModel:GetDescendants()
            for i = 1, #Cache do
                if Cache[i]:IsA("Part") or Cache[i]:IsA("MeshPart") or Cache[i]:IsA("UnionOperation") then
                    Cache[i].BrickColor = ReleventColor
                end
            end
        else
            GenerateSelectionModel()
        end
        if Qdown and not Edown then
            CurrentRotation = CurrentRotation + 1
        elseif Edown then
            CurrentRotation = CurrentRotation - 1
        end
    end
end

local function BuildEquipped()
    Equipped = true
    if SelectionModel then
        print("GeneratingSelection")
        GenerateSelectionModel()
    end
    if not SelectionModel then
        print("Parenting")
        SelectionModel.Parent = ObjectsFolder
        Mouse.TargetFilter = SelectionModel
    end
    RunService:BindToRenderStep("FrameRendered", 1, FrameRendered)
    ContextService:BindAction("RotateLeft", function(_, inputState)
        Qdown = inputState == Enum.UserInputState.Begin
    end, true, "q")
    ContextService:BindAction("RotateRight", function(_, inputState)
        Edown = inputState == Enum.UserInputState.Begin
    end, true, "e")
end

local function BuildUnequipped()
    Equipped = nil
    if SelectionModel then
        SelectionModel:Destroy()
    end
end

local function Initiate()
    spawn(function()
        wait()
        print(Equipped)
        if Equipped then
            print("Equipped")
            BuildEquipped()
        else
            print("Dequipped")
            BuildUnequipped()
        end
    end)
end

Tool.Unequipped:Connect(function()
    CloseUIs()
    BuildUnequipped()
end)

GUI.Buildings.MouseButton1Click:Connect(function()
    GUI.Enabled = false
    BuildingUI.Enabled = true
    for i,v in pairs(BuildingUI.ScrollingFrame:GetChildren()) do
        if v:IsA("TextButton") then
            v.MouseButton1Click:Connect(function()
                if not db1 then
                    db1 = true
                    SelectionModel = Tool.Objects.Buildings[v.Name]
                    Initiate()
                    Equipped = true
                    CloseUIs()
                    db1 = false 
                end
            end)
        end
    end
end)

GUI.Navy.MouseButton1Click:Connect(function()
    GUI.Enabled = false
    NavyUI.Enabled = true
    for i,v in pairs(NavyUI.ScrollingFrame:GetChildren()) do
        if v:IsA("TextButton") then
            v.MouseButton1Click:Connect(function()
                if not db1 then
                    db1 = true
                    SelectionModel = Tool.Objects.Navy[v.Name]
                    Initiate()
                    Equipped = true
                    CloseUIs()
                    db1 = false 
                end
            end)
        end
    end
end)

GUI.Infantry.MouseButton1Click:Connect(function()
    GUI.Enabled = false
    InfantryUI.Enabled = true
    for i,v in pairs(InfantryUI.ScrollingFrame:GetChildren()) do
        if v:IsA("TextButton") then
            v.MouseButton1Click:Connect(function()
                if not db1 then
                    db1 = true
                    SelectionModel = Tool.Objects.Infantry[v.Name]
                    Initiate()
                    Equipped = true
                    CloseUIs()
                    db1 = false 
                end
            end)
        end
    end
end)

local Infantry = script.Parent.Objects.Infantry:GetChildren()
local Navy = script.Parent.Objects.Navy:GetChildren()
local Buildings = script.Parent.Objects.Buildings:GetChildren()
for i = 1, #Infantry do
    local frame  = Instance.new("TextButton")
    frame.Name   = Infantry[i].Name
    frame.Text   = Infantry[i].Name
    frame.BackgroundColor3 = Color3.fromRGB(60, 120, 180)
    frame.Font = "SourceSansBold"
    frame.TextSize = 30
    frame.Parent = InfantryUI.ScrollingFrame
end
for i = 1, #Navy do
    local frame  = Instance.new("TextButton")
    frame.Name   = Navy[i].Name
    frame.Text   = Navy[i].Name
    frame.BackgroundColor3 = Color3.fromRGB(60, 120, 180)
    frame.Font = "SourceSansBold"
    frame.TextSize = 30
    frame.Parent = NavyUI.ScrollingFrame
end
for i = 1, #Buildings do
    local frame  = Instance.new("TextButton")
    frame.Name   = Buildings[i].Name
    frame.Text   = Buildings[i].Name
    frame.BackgroundColor3 = Color3.fromRGB(60, 120, 180)
    frame.Font = "SourceSansBold"
    frame.TextSize = 30
    frame.Parent = BuildingUI.ScrollingFrame
end
0
*Edit* I fixed the issue but now it just clones +1 for each time the tools is equipped. :/ AnnaVonWachstein 7 — 4y

Answer this question