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

How do I make my model invisible on touch?

Asked by 4 years ago

I have my script, (https://gyazo.com/f5850a440fc6ec5ce3aa4978a366b089) and it's meant to make a Model of mine invisible when touched, however, the script doesn't work and errors.(https://gyazo.com/50df5e4020f74742ea6a163f99318950). Can someone please tell me how I messed up?

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

To do that you would have to Fire an event, for this you would have to use "Touched", What you did was only defining a function, if you want the function to get called when something happens, you need to use fire an event. The way you do this is by:

local model = script.Parent.Parent
local Connected

for _, child in pairs(model:GetChildren()) do
    if child:IsA("BasePart") then
        Connected = child.Touched:Connect(function(Hit)
            child.Transparency =0
            Connected:Disconnect()
        end)
    end
end

You might have noticed that I placed a new variable there called "Connected", then I defined the variable to the Touched event, and later disconnect the variable inside the event. I do this with touched event so, if it was touched, it will just do it one time and then wait until the part get away and retouch it again, or until it get touched again by something. This way the touched event doesn't fire multiple times.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The answer is simple. You're setting the transparency to 0, which would make it opaque (visible). A transparency of 1 however, will make the part(s) invisible.

Also since you didn't provide the entirety of the script I had to change the function a bit.

local model = script.Parent.Parent

script.Parent.Touched:connect(function(hit)
    print("test3")
    for name, child in pairs(model:GetChildren()) do
        if child.IsA("BasePart") then
            child.Transparency = 1
        end
    end
    print("test4")
end)

Answer this question