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)
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
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.
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!
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
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