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!