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

How do I script a Grouped Model?

Asked by 5 years ago

I have a model which is made of grouped parts, and for each part, I've scripted them so that when touched, it will disappear. My problem is, I can't seem to script the whole model so that when you touch one part, the rest will disappear with it. Could someone help me? I really need this whole model to disappear and reappear.

2 answers

Log in to vote
0
Answered by 5 years ago

Since a group is a collection of parts, you will need to loop through the parts and manually change their transparency to 1. For this example, we will be using the "for i,v in pairs" loop (https://developer.roblox.com/articles/For-Loops). First, we will need to get the parts in the model for the loop to cycle through. We can the parts (otherwise known as "Children") of a model by using a built-in roblox feature ":GetChildren" (https://developer.roblox.com/api-reference/function/Instance/GetChildren). After that, we will need to create the variables and assign one the table of parts. In all, it should look like this.

local model = game.Workspace.Model --Change this to the location of your model
local childrenInModel = model:GetChildren()

Next, we will need to add the "for i,v in pairs" loop. Now that we have table of parts in the model, we can go on and set the loop. It should look similar to this.

for i,v in pairs(childrenInModel) do
    --Check if part
end

Since anything besides parts can be in a model, we will need to check whether the part is an actual part. We can do this by adding another roblox built-in feature ":IsA" (https://developer.roblox.com/api-reference/function/Instance/IsA). Basically, this feature checks whether the part is what you want to check it is. So we would ":IsA" like this.

for i,v in pairs(childrenInModel) do
    if v:IsA("Part") then
        --Change Transparency
    end
end

Finally, we will need to change the transparency of the object to make it "disappear". This is as easy as simply writing "v.Transparency = 1". So in the end, your code should look similar to this.

local model = game.Workspace.Model --Change this to the location of your model
local childrenInModel = model:GetChildren()

for i,v in pairs(childrenInModel) do
    if v:IsA("Part") then
        v.Transparency = 1
    end
end
0
Thank you so much! I appreciate the help, and sorry for the late reply. I'll try this script out. Thank you! :D Aprilsaurus 48 — 5y
Ad
Log in to vote
0
Answered by
maxbd 44
5 years ago
Edited 5 years ago

When you say 'Disappear' do you mean go completely transparent and have the CanCollide field turned off, or do you mean destroyed?

You could try something along the lines of this and modify to what you want:

-- Script (on server, inside part inside model)

script.Parent.Touched:Connect(function()
    for i,v in next, script.Parent.Parent:GetDescendants() do
        v:Destroy()
    end
end)

A simple explanation: when the parent of the script (in this case the part inside the model) is touched, loop through the parent's parent (in this case, the model) and destroy every object inside of the model.

0
By 'disappear', yes, I mean for it to go fully transparent and CanCollide is off (so that anyone can go through it). I also don't want it to appear until the player is no longer touching it, I've tried a script on a brick as a test and noticed that it kept disappearing and reappearing while still touching it. And thank you so much for the help, I greatly appreciate it! :) Aprilsaurus 48 — 5y

Answer this question