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

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

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.

1 comment

0

click is a sound effect by the way

1 answer

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)

2 comments

0

It doesnt work for all of the lights, only one.

0

Should i use getdecendants instead?

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