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

[Fixed] Why does this table become nil when I never set it to nil?

Asked by 6 years ago
Edited 6 years ago

So, I am trying to make a script that can make a spring constraint hook onto your arm and what your mouse is at. So far, I got this

local lassos = {}
local lasso = game.ReplicatedStorage.MakeLasso
local mouse = game.Players.LocalPlayer:GetMouse()
local gui = Instance.new('ScreenGui', game.Players.LocalPlayer.PlayerGui)
gui.Enabled = false

local function MakeButton(instance) --some UI to destroy constraints
    print(instance:GetFullName())
    local new = Instance.new('TextButton', gui)
    new.TextScaled = true
    new.TextWrapped = true
    new.Text = instance.Attachment1.Parent.Parent.Name..'.'..instance.Attachment1.Parent.Name
    new.Style = Enum.ButtonStyle.RobloxButtonDefault
    new.TextColor3 = Color3.new(1, 1, 1)

    new.MouseButton1Click:Connect(function()
        if instance.Attachment0 == nil then
            new:Destroy()
            return
        end
        instance.Attachment0:Destroy()
        instance.Attachment1:Destroy()
        instance:Destroy()
        new:Destroy()
    end)
end

do --makes gui
    local gc = Instance.new('UIGridLayout', gui)
    gc.CellPadding = UDim2.new(0, 10, 0, 10)
    gc.CellSize = UDim2.new(0, 100, 0, 50)
end
script.Parent.Parent.Activated:Connect(function(mouse)
    local mouse = game.Players.LocalPlayer:GetMouse()
    local plyr = script.Parent.Parent.Parent
    local hand = plyr:FindFirstChild('RightHand') or plyr:FindFirstChild('Right Arm')
    if mouse.Target and mouse.Target:IsA('BasePart') and hand then
        local a0 = hand
        local a1 = mouse.Target
        local lasso = lasso:InvokeServer(a0, a1, 'Create')
        if lassos == nil then lassos = {} return end
        for _, object in pairs(lasso) do
            table.insert(lassos, object)
        end
        MakeButton(lasso[1])
    end
end)

script.Parent.Parent.Unequipped:Connect(function()
    for _, object in pairs(lassos) do
        lasso:InvokeServer(object, '', 'Destroy')
    end
    lassos = {}
    for _, obj in pairs(gui:GetChildren()) do
        if obj:IsA('Frame') then
            obj:Destroy()
        end
    end
end)

local user = game:GetService('UserInputService')
local deb = false

user.InputBegan:Connect(function(key)
    local target = mouse.Target
    if not deb and key.KeyCode == Enum.KeyCode.T and user:GetFocusedTextBox() == nil and target ~= nil and target:IsA('BasePart') then
        deb = true
        if lassos == nil then lassos = {} deb = false return end
        for i, tie in pairs(lassos) do
            if tie:IsA('SpringConstraint') then
                print('s')
                local a0 = target
                if tie.Attachment1.Parent then
                    lasso:InvokeServer(a0, tie.Attachment1.Parent, 'Create')
                    tie.Attachment0:Destroy()
                    tie.Attachment1:Destroy()
                    tie:Destroy()
                end
            end
        end
        lassos = {}
        deb = false
    elseif key.KeyCode == Enum.KeyCode.F2 then
        gui.Enabled = not gui.Enabled
    end
end)

I know this is kinda bad remote event security, but I just want to play around with constraints.

I thought this would work, but when I run it, it errors Players.Player1.Backpack.Lasso.Handle.Lasso:42: bad argument #1 to 'pairs' (table expected, got nil). Nowhere in the script sets the table to nil. Also, I did if lassos == nil then lassos = {} end statements to see if that fixed it, but for some reason it does not. Have any ideas?

0
if lassos == nil then lassos = 1 end <-- try making that 1?? idk, I am dumb greatneil80 2647 — 6y
0
but it needs a table.... 1 is not a table......... im confused hiimgoodpack 2009 — 6y
0
Oh, like I was saying.. I am dumb T_T greatneil80 2647 — 6y
0
if there was a few more comments, it would be helpful to understand of what you are specifically trying to achieve. also on line 34 you redefined 'mouse' even though it was already defined on line 3. abnotaddable 920 — 6y
View all comments (7 more)
0
Also, in the meantime try lots of print debugging. find out whats not working in your code. abnotaddable 920 — 6y
0
"local lasso = lasso:InvokeServer(a0, a1, 'Create')", line 40. Does this return anything worth mentioning? Remember than line 42 iterates over "lasso" XAXA 1569 — 6y
0
@hiimgoodpack Make sure you have spelt the difference between 'lasso' and 'lassos' correctly! abnotaddable 920 — 6y
0
@adnotaddable it kept saying that mouse was not defined on line 34 so i just redefined it @XAXA it just returns a rope and two attachments hiimgoodpack 2009 — 6y
0
also @adnotaddable its not that it does not run in specific areas, it is that it errors. hiimgoodpack 2009 — 6y
0
for line 42 you are doing for _, object in pairs(lasso) do maybe it should be for _, object in pairs(lassos) do because you are creating the table lassos the line before and lasso appears to be a remotefunction abnotaddable 920 — 6y
0
whoops hiimgoodpack 2009 — 6y

Answer this question