Help With Obby Staircase

This is just a simple script for my obby staircase and I am getting attempt to index nil with connect

local Part = script.Parent:GetDescendants()

Debounce = false

function onTouch()

	if Debounce == false then

		Debounce = true

		for i = 1, 20 do

			Part.Transparency = i / 20

			task.wait(0.1)

		end

		Part.CanCollide = false

		task.wait(2)

		Part.CanCollide = true

		Part.Transparency = 0

		Debounce = false

	end

end

Part.Touched:connect(onTouch)

Connect needs to be uppercase not lower. You have “connect” you need “Connect”

that doesnt fix my problem but you are right i meant to make it uppercase

The Part variable is initialized as an array of script.Parent’s descendants.
Specify your part to a specific one.


Or you could loop at them:

for _, v in Part so
    if not v:IsA("BasePart") then
        continue
    end

    v.Touched:Connect(onTouch)
end

How can I make it so all descendants are affected rather than just one

for i, v in pairs(script.Parent:GetDesendants() do -- get all of the parts
     if not v:IsA("BasePart") then return end -- return if v isnt a part
     v.Touched:Connect(onTouch) -- connect the function on touch
end

Edit: just realized that @DiscoDino01 did the same thing lol

Replace the return with continue.
return terminates the loop immediately after a non-BasePart instance.

1 Like

If you’re going to do this, you must make it asynchronous. The waits will make each iteration of the loop have a delay.

I’m confused what do I need to change to make one script get all the parts instead of a specific part

edit forgot to mention i am a fairly new scripter

hi . Basically, when you use function :GetDecendence() , the function will return a table which means the variable “Part” is actually a table . Think of it as an excel sheet . The Excel sheet stores the part variable in each row. So , you need to write a code that loop through the table . you can do it by using for i,e in pairs(Part) . That statement is called a for loop and it continues until it reach the end of the table . i and e in the for loops are variable, you can name them anything. variable i gives you the amount of time the loop ran and e gives you the value in the table which in this case, is the individual part .

Hope this is not too complicated lol . If you have any questions, feel free to ask

:GetDescendants() returns a table, not a Part Instance. I don’t really know what your intention is behind that script but I would suggest either assigning the Part variable to a single part, or use for loops to handle all the parts at once, like this:

local Part = script.Parent:GetDescendants()
local Debounce = false
function onTouch() --Same variables and functions here

	if Debounce == false then

		Debounce = true

		for i = 1, 20 do

			Part.Transparency = i / 20

			task.wait(0.1)

		end

		Part.CanCollide = false

		task.wait(2)

		Part.CanCollide = true

		Part.Transparency = 0

		Debounce = false

	end

end
for i,v in ipairs(Part) do --Only difference is that we're using for loops to connect events.
       if v:IsA("BasePart") then
              v.Touched:Connect(onTouch)
       end
end

I would recommend using this script if this is what you want to achieve.

1 Like

trying to make staircase you step on parts they dissapear i didnt think i would have this much trouble making this lol

but that doesnt work

i mean i could put a script in every part but i want to do it with once script to avoid lag

Ah I see, it’s a debounce issue. Here’s the fixed code:

local Part = script.Parent:GetDescendants()
for i,v in ipairs(Part) do
       if v:IsA("BasePart") then
              local Debounce = false
              v.Touched:Connect(function()
                            if Debounce == false then

		Debounce = true

		for i = 1, 20 do

			Part.Transparency = i / 20

			task.wait(0.1)

		end

		Part.CanCollide = false

		task.wait(2)

		Part.CanCollide = true

		Part.Transparency = 0

		Debounce = false

	end
              end)
       end
end
--Did you mean get childrens?
--local Parts = script.Parent:GetDescendants()

local Parts = script.Parent:GetChildrens()

Debounce = false

function onTouch(part)
	if not game.Players:GetPlayerFromCharacter(part Parent) then return end
	if Debounce == false then

		Debounce = true

		for i = 1, 20 do

			Part.Transparency = i / 20

			task.wait(0.1)

		end

		Part.CanCollide = false

		task.wait(2)

		Part.CanCollide = true

		Part.Transparency = 0

		Debounce = false

	end

end

--Bind all parts to the function
for _, v in Parts do
	v.Touched:Connect(onTouch)
end

If it worked for you, don’t forget to mark the post as solution so others don’t waste their time!

@Boeufplume6771 @zaydoudou both of your scripts do not work

seriously i am confused am i missing something im trying to make the classic obby stairs

well i’m going to assume your stair step parts (and script) are inside a model

local TS = game:GetService("TweenService") -- Use TweenService instead of a for loop
local tInfo = TweenInfo.new(--[[Don't forget your TweenInfo]])

local Parts = script.Parent:GetDescendants() -- If the steps are in their own models, use :GetDescendants()

for _, Part in Parts do
  if not Part:IsA("BasePart") then continue end -- Make sure part is a step

  local Debounce = false

  Part.Touched:Connect(function(Hit)
    if not game.Players:GetPlayerFromCharacter(Hit.Parent) then return end -- Check if the player touched the part
    if Debounce then return end  -- Check if the 'cooldown' is active
    Debounce = true -- Begin 'cooldown'

    TS:Create(Part, tInfo, {["Transparency"] = 1}):Play() -- Fade transparency
    Part.CanCollide = false

    task.wait(2)

    Part.CanCollide = true
    Part.Transparency = 0

    Debounce = false -- End 'cooldown'
  end)
end

edit: forgot to move debounce into the loop

1 Like
local Parts = script.Parent:GetDescendants()

local Debounce = false

function onTouch()
	if not Debounce then
		Debounce = true
		for _, Part in ipairs(Parts) do
			for i = 1, 20 do
				Part.Transparency = i / 20
				task.wait(0.1)
			end
			Part.CanCollide = false
			task.wait(2)
			Part.CanCollide = true
			Part.Transparency = 0
		end
		Debounce = false
	end
end

for _, Part in ipairs(Parts) do
	Part.Touched:Connect(onTouch)
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.