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

Why does my getchildren line of code keep saying: Attempt to call a nil value?

Asked by 3 years ago

I'm attempting to make a horror game on Roblox, when you interact with this light switch its supposed to turn them on, but whenever I try it doesn't work, and the output says,"Workspace.Lights.Part.Script:3: attempt to call a nil value" what does this mean?

my code:

script.Parent.ProximityPrompt.Triggered:Connect(function()
    local light = script.Parent.Parent:GetChildren("Light")
    local pointlight = light:GetChildren("PointLight")
    pointlight.Enabled = true
    local click = light:GetChildren("Click")
    click:Play()
    script.Parent.ProximityPrompt.Enabled = false
end)

please help.

0
click is a sound effect by the way SirSuperScorpion 27 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

For getting a object use :WaitForChild().

Here is the fixed code

script.Parent.ProximityPrompt.Triggered:Connect(function()
    local light = script.Parent.Parent:WaitForChild("Light")
    local pointlight = light:WaitForChild("PointLight")
    pointlight.Enabled = true
    local click = light:WaitForChild("Click")
    click:Play()
    script.Parent.ProximityPrompt.Enabled = false
end)
0
It doesnt work for all of the lights, only one. SirSuperScorpion 27 — 3y
0
Should i use getdecendants instead? SirSuperScorpion 27 — 3y
Ad

Answer this question