Nil value in this script?

I’ve created a waterslide and I need the player to go down it, so I created this velocity script that makes each part of the slide have a certain amount of velocity

local slideparts = script.Parent:GetChildren()
for i = 1, #slideparts do
	local slidepart = slideparts[i]
	local union = slidepart:FindFirstChild("Tube")
	wait(0.1)
	union.Velocity = Vector3.new(0, 0, 80)
	print("eat") --Debugging purposes
end

I know this most likely a simple fix but my minds gone blank, any solutions?
I keep getting this error:
Workspace.Model.Model.Slide:7: attempt to index local ‘union’ (a nil value)

image

The error means there is no object inside slidepart called “Tube”. Can you confirm that there is indeed a “Tube” part in every one of those tube slides?

1 Like

image @Qxest

GetChildren only gets one layer of instances. You need to use GetDescendants()

So GetChildren only sees, Slide, Tube Slide, etc… and not Model, union, tube.

local slideparts = script.Parent:GetDescendants()
for i = 1, #slideparts do
    if(slideparts[i].Name == "Tube")then
        slideparts[i].Velocity = Vector3.new(0, 0, 80)
        print("eat")
    end
end

Your script is taking the Slide script as one of the objects.
Then you are telling it to find “Tube” inside of it.
Then, without checking whether or not it actually found a “Tube”, you are asking it to set it’s velocity.
You should either check that the object it’s searching is a model (not a script) or better yet add a if statement after the wait(0.1) verifying that union exists.

Simply
if union then

1 Like

knew it was a simple mistake, my bad.

1 Like