Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I get this script to find items specified in a table in the workspace?

So I just typed up a script that is supposed to detect when a character is within a certain distance from a part, then prompt the user to interact. What I'm having trouble with is getting the script to detect one of the parts in the workspace by using a table. Here's the code:

local player = game.Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()
local cam = workspace.CurrentCamera
local Detectables = workspace:WaitForChild("Detectables")

local ObjectDetected = false
local DetectedObject

local DetectableObjs = {}

for _, v in pairs(Detectables:GetChildren()) do
    table.insert(DetectableObjs, v)    
end

game:GetService('RunService').RenderStepped:connect(function()
    if (char:WaitForChild("Torso").Position - workspace:WaitForChild(DetectableObjs.Name).Position).magnitude <= 10 then
        player.PlayerGui:WaitForChild("Interact"):WaitForChild("Frame").Visible = true
    elseif (char:WaitForChild("Torso").Position - workspace:WaitForChild(DetectableObjs.Name).Position).magnitude > 10 then
        player.PlayerGui:WaitForChild("Interact"):WaitForChild("Frame").Visible = false
    end
end)


mouse.KeyDown:connect(function(Key)
    if Key == "f" and ObjectDetected then
        DetectedObject.Parent = player.Backpack
        ObjectDetected = false
        ObjectDetected = nil
    end

I keep getting the error: Argument 1 missing or nil

3 comments

0

I'm new at scripting, but, you didn't define DetectedObject at line 8. Unless, that is supposed to be like that. I'll just make that a wild guess.

0

DetectedObject is supposed to be nil until a part that's defined in the table is found in the workspace.

0

You don't need to assign variables a value when you define them, so it works. What line is the error on?

1 answer

✓ Accepted answer

Your trying to get the position of each object.. but here... DetectableObjs is an array.. so you need to enter a key.

DetectableObjs[INDEX].Name).Position

   if (char:WaitForChild("Torso").Position - workspace:WaitForChild(DetectableObjs[INDEX].Name).Position).magnitude <= 10 then
        player.PlayerGui:WaitForChild("Interact"):WaitForChild("Frame").Visible = true
    elseif (char:WaitForChild("Torso").Position - workspace:WaitForChild(DetectableObjs[INDEX].Name).Position).magnitude > 10 then
        player.PlayerGui:WaitForChild("Interact"):WaitForChild("Frame").Visible = false
    end

[UPDATED Code]

local player = game.Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()
local cam = workspace.CurrentCamera
local Detectables = workspace:WaitForChild("Detectables")

local ObjectDetected = false
local DetectedObject

local DetectableObjs = {}

for _, v in pairs(Detectables:GetChildren()) do
    table.insert(DetectableObjs, v)    
end

game:GetService('RunService').RenderStepped:connect(function()
	for _, v in pairs(DetectableObjs) do
		if (char:WaitForChild("Torso").Position - workspace:WaitForChild(v.Name).Position).magnitude <= 10 then
        player.PlayerGui:WaitForChild("Interact"):WaitForChild("Frame").Visible = true
    elseif (char:WaitForChild("Torso").Position - workspace:WaitForChild(v.Name).Position).magnitude > 10 then
        player.PlayerGui:WaitForChild("Interact"):WaitForChild("Frame").Visible = false
    end
	end
end)


mouse.KeyDown:connect(function(Key)
    if Key == "f" and ObjectDetected then
        DetectedObject.Parent = player.Backpack
        ObjectDetected = false
        ObjectDetected = nil
    end

This discussion is preserved as it was. New answers, comments, and votes are closed.