Hey everyone! I made a script today that’s basically disabling a script when a part is touched. I don’t know what’s wrong with it because it does detect touching but it says this:
local Part = script.Parent
local Movingparts = workspace.SkiLift.Top.Conv:GetDescendants()
local Movingparts2 = workspace.SkiLift.Cable:GetDescendants()
Part.Touched:Connect(function()
if Movingparts:IsA("Script") then
Movingparts.Disabled = true
end
if Movingparts2:IsA("Script") then
Movingparts2.Disabled = true
end
end)
Rather then use GetDescedents that get’s the children of the Instance and also the childrens chilldren of the part…I would use GetChildren/FindFirstChild
Movingparts and Movingparts2 are tables. Instead, do this:
local Part = script.Parent
local Conv = game.Workspace.SkiLift.Top:WaitForChild("Conv")
Part.Touched:Connect(function()
for _, descendant in pairs(Conv:GetDescendants()) do
if descendant:IsA("Script") then
descendant.Disabled = true
end
end
end)
You can add as many scripts as you want, they’ll all be disabled.
It works, but it doesn’t do what I thought it did because the ski lines are basically conveyor belts so I thought disabling the scripts that make things move on top of it would make everything stop but it didn’t, So Instead I wanted to make it so that if the part is touched, the children of a specific model named “Moving” that is in all cable cars would be anchored
I tried doing this but it sent this error:
local Part = script.Parent
local Conv = game.Workspace.SkiLift:WaitForChild("Siège")
Part.Touched:Connect(function()
for _, descendant in pairs(Conv:GetDescendants()) do
if descendant.Name == ("Moving") then
local parts = descendant:Getchildren()
for index, parts in pairs(parts) do
parts.Anchored = true
end
end
end
end)
You have two problems here, one that will become apparent shortly.
The first being that you are attempting to call an inexistent member function of an object “Getchildren” which instead should be :GetChildren which will give you all of the contents held within that object.
Another issue which I foresee you running into with this code is that you named one of your for loop tracker variables “parts” which shares a name with an earlier utilized variable, resulting in unexpected behavior, and is bad practice if it were to function as expected, because there is ambiguity on behalf of the reader.
Of course! If you need any other assistance don’t hesitate to ask, otherwise I recommend you mark the answer that you got the most usage from as the solution which will allow future users with potentially similar issues to learn from this post.
I will! Just one more thing and I hope I’m not bothering you. I want to make it so that when it’s anchored, after 5 seconds it gets unanchored and when it’s the cable car that’s behind the current one’s turn it does the same thing over and over again. So basically it stops everytime a car touches it and then moves again, like a cycle
It stops when the part gets touched, But I want to make it so that after 5 seconds they keep going until it’s the turn of the next cable car and then it stops again and the same basically happens for every car and it keeps going.
There are many way of doing this. The least trivial way is to just make use of task.wait(5) after your loops that anchor the car end. Than running another loop to unanchor the parts.
One thing I recommend you add to this code is some kind of guard clause (something that prevents unnecessary/extra code from running if it doesn’t need to) in order to stop this block of code from running more than once.
Best way of doing it is creating a variable called liftCarUpdate (assuming you are updating a lift cab by anchoring it) which will be set to true when you anchor the car, after 5 seconds set to false. And at the top of the touched event use a conditional (if statement) to check it it’s already processing one of the cars within that 5 second interval.
I tried doing this but after doing so it doesn’t even stop in the first place
local Conv = game.Workspace.SkiLift:WaitForChild("Siège")
Part.Touched:Connect(function()
for _, descendant in pairs(Conv:GetDescendants()) do
if descendant.Name == ("Moving") then
local MovParts = descendant:GetChildren()
for index, MovParts in pairs(MovParts) do
MovParts.Anchored = true
task.wait(5)
for _, descendant in pairs(Conv:GetDescendants()) do
if descendant.Name == ("Moving") then
local MovParts = descendant:GetChildren()
for index, MovParts in pairs(MovParts) do
MovParts.Anchored = false
end
end
end
end
end
end
end)
Please remember as I had mentioned, you want to add those additional loops after the groups of code that anchor the parts the first time, you do not want to nest the loops that unanchor the parts inside the loops that anchor them.
Some pseudo code to substantiate my words here …
for i,v in pairs movparts do
v.Anchored = true
end
task.wait(5)
for i,v in pairs movparts do
v.Anchored = false
end