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

attempt to index a "nil" function?

Asked by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Hello, I'm trying to write a Pick-upScript as some people may know, I'm slowly making it Universal, so I can finish it as an InventorySorter, I just need to test it a bit, so I'm writing it to access a predefined item called Rifle.

I moved the ClickDetector and forgot that I had done so during testing, once I got the error, I immediately knew what I had done, therefore I decided to debug this issue by writing the first Universal component, the function to find the ClickDetector inside of the Clicked item; 'getClickDetector(item)'. I'm not exactly sure what I've done wrong, could someone help me?

error: Players.simplealgorithm.PlayerScripts.Pick-upItem:4: attempt to call global 'getClickDetector' (a nil value)

function getClickDetector(item)
    local itemDecendants = item:GetDescendants()
    for _,clickDetector in ipairs(itemDecendants) do
        if (clickDetector:IsA("ClickDetector") and clickDetector.Parent:IsA("BasePart") and clickDetector:IsDescendantOf(item)) then
            print(clickDetector:GetFullName()); return clickDetector
        end
    end
end

local userInputService = game:GetService("UserInputService")
local rifle = workspace:WaitForChild("Rifle")
local inventory_ = script:WaitForChild("inventory")
local clickDetector = getClickDetector(rifle)
local toInventory = false
clickDetector.MouseClick:Connect(function(player)
    local character = player.Character or player.CharacterAdded:wait()
    if (player and character) then
        local inventory = inventory_:Clone(); inventory.Parent = character; inventory_:Destory()
        userInputService.InputBegan:Connect(function(input, gameProcessed)
            if (userInputService:IsKeyDown(Enum.KeyCode.E) and not gameProcessed) then
                toInventory = true
                if (toInventory and rifle) then
                    rifle.Parent = character:WaitForChild("inventory")
                    rifle.Anchored = true
                end
            end
        end)
        if not (toInventory) then
            if (character and rifle) then
                local torso = character:FindFirstChild("Torso")
                if (rifle and torso) then
                    local weld = Instance.new("Weld"); weld.Name = "weld"
                    rifle.CFrame = torso.CFrame * CFrame.new(0,5,0) * CFrame.Angles(0,45,0)
                    rifle.Achored = false
                    weld.Part0 = torso
                    weld.C0 = torso.CFrame:Inverse()
                    weld.Part1 = rifle
                    weld.C1 = rifle.CFrame:Inverse()
                    weld.Parent = rifle
                end
            end
        end
    end
end)

local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local mousePos = mouse.Hit.p
userInputService.InputBegan:Connect(function(input, gameProcessed)
    if (userInputService:IsKeyDown(Enum.KeyCode.E) and not gameProcessed) then
        if (input.UserInputType == Enum.UserInputType.MouseButton2) then
            local weld = rifle:FindFirstChild("weld")
            weld:Destroy()
            rifle.Position = Vector3.new(mousePos)
            rifle.Achored = true
        end
    end
end)

Thank you, if you could also pinpoint some other issues or efficiency probabilities in my Script that would be nice too!

0
It literally means that it doesn't have the function, check your universal script to make sure you did everything correctly rokedev 71 — 5y
0
I have, that's the point, the whole thing is up there Ziffixture 6913 — 5y
0
your variable is referencing something that doesnt yet exist. DinozCreates 1070 — 5y
0
try moving the click detector function before your variables and see if that does anything DinozCreates 1070 — 5y
View all comments (9 more)
1
only put the function of getClickDetector before clickDetector variable yHasteeD 1819 — 5y
1
i just said that. DinozCreates 1070 — 5y
0
why would that not be the very first thing u think of Gey4Jesus69 2705 — 5y
0
It was, but it's Global, it shouldn't be a problem Ziffixture 6913 — 5y
0
did you try it DinozCreates 1070 — 5y
0
I did, it's fixed now though, i should've tried it before, it was still broken but i fixed it. only nothings happening now, nothings running either Ziffixture 6913 — 5y
0
I've updated the Script above Ziffixture 6913 — 5y
0
It's got the ClickDetector, but nothings running, the weapon isn't moving either:/ help? Ziffixture 6913 — 5y
0
I'm not exactly sure if this relates to the issue as well, bu every time i click the screen, my mouse freezes in place, it's not the only time I can see the mouse again is if it's outside of the screen, so I can press the stop button Ziffixture 6913 — 5y

Answer this question