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

Script parents aren't being located. Can someone help?

Hello I've got a problem with locating a certain model

I've got a button which toggles off/on strobe lights for a plane. I've got a GUI in a first officers seat, when you sit in the seat you get the gui, the button is in the GUI.

The button contains this code:

kit = script.Parent.Parent.Parent.Parent.Parent.Parent.StrobeLights

button = script.Parent

toggle = true

button.MouseButton1Down:connect(function()
	
	

if toggle == false then	
    toggle = true
	
	else 
while toggle == true do
	
wait(0.01)	
kit.LightA.PointLight.Enabled = true
kit.LightA.BillboardGui.Enabled = true
wait(.05)
kit.LightA.PointLight.Enabled = false
kit.LightA.BillboardGui.Enabled = false

wait(0.05)
kit.LightA.PointLight.Enabled = true
kit.LightA.BillboardGui.Enabled = true
wait(0.05)
kit.LightA.PointLight.Enabled = false
kit.LightA.BillboardGui.Enabled = false
wait(1)
end
	end
	
end)

button.MouseButton2Down:connect(function()
	toggle = false
end)

The problem is, the script locates it's Parents before it's equipped and it locatesthem after. Even though other codes uses the exact same line:

kit = script.Parent.Parent.Parent.Parent.Parent.Parent.StrobeLights

Is there a way to make it locate only before it's equipped?

6 comments

0

What are all of those parents for?

0

There parts in "StrobeLights" which have lights in them, so the script is made to toggle off/on the strobe effect

0

Have you heard of variables? https://www.lua.org/pil/1.html

0

Why do you have lights in lights? Couldn't you move this script up

0

yes roblox might misinterpret code bc you have all those "Parents". Define the direcoty using something like:game.Workspace.Part and not script.Parent.Parent forever and ever. Also do you know what varibles are?

1 answer

Hello

If you want to accurately locate a model you should do:

game:GetService('Workspace')['MODELNAMEHERE']

You can also use:

script:FindFirstAncestor('MODELNAMEHERE')

If your script is located under a lot of models/parts/folders etc. you should not use script.Parent.Parent.Parent…... because

a) This will create a lot of mess in your code.

b) You are not sure if the parent you are locating is actually the model you want to locate.

~Frag

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