Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

The output says that there are no problems with this code, but it doesn't work???

Asked by 5 years ago
Edited 5 years ago

I tried to make a code for a chair that disappears once you click it and you can't touch it. I played my game but then only the seat disappeared when I clicked it. Here is my code:

local Part = script.Parent:GetChildren("Part")
local Seat = script.Parent:FindFirstChild("Seat")

function onClick()
        Part.Transparency = 1
        Part.CanCollide = false
        Seat.Transparency = 1
        Seat.CanCollide = false
    end
script.Parent.ClickDetector.MouseClick:connect(onClick) 

I put it into the output and it said that there were no errors. Help??

P.S. I should mention that there are many parts, not just one.

0
On line 10 use :Connect(), :connect() is deprecated. Fad99 286 — 5y

4 answers

Log in to vote
0
Answered by 5 years ago

Try doing this, it basically gets all the parts from script.Parent and changes their properties

local n = script.Parent:GetChildren()

for i, v in pairs(n) do
    if v:IsA("Part") then
        v.Transparency = 1
        v.CanCollide = false
    end
end
0
Oh thanks! It worked but at first I had to write "function onClick()". n00b_warri0r 11 — 5y
Ad
Log in to vote
0
Answered by
Fad99 286 Moderation Voter
5 years ago
Edited 5 years ago
local Part = script.Parent:WaitForChild("Part")
local Seat = script.Parent:FindFirstChild("Seat")

function onClick()
        Part.Transparency = 1
        Part.CanCollide = false
        Seat.Transparency = 1
        Seat.CanCollide = false
    end
script.Parent.ClickDetector.MouseClick:connect(onClick) 

You used :GetChildren() instead of something like:WaitForChild(). :GetChildren() Would Return a Table of all the children while :WaitForChild() would wait for that object to load before proceeding the rest of the code.

0
Didn't work. n00b_warri0r 11 — 5y
Log in to vote
0
Answered by
KDarren12 705 Donator Moderation Voter
5 years ago

GetChildren() gets all of the children in an object.

local Part = script.Parent:FindFirstChild("Part")
local Seat = script.Parent:FindFirstChild("Seat")

function onClick()
    Part.Transparency = 1
    Part.CanCollide = false
    Seat.Transparency = 1
    Seat.CanCollide = false
    end
script.Parent.ClickDetector.MouseClick:Connect(onClick)
0
No only one of the parts disappeared. n00b_warri0r 11 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I played your game and I know what you mean. Try doing this. When you click on the chair, destroy the chair. Then add a script that puts the chair in your backpack.

local model = [The model of the chair]

function onClick()
    model:Destroy()
    end
script.Parent.ClickDetector.MouseClick:connect(onClick) 

Answer this question