Hello,
I want to unanchor many parts in a model, but don’t know an efficient way to do it. I know I could use a for loop but that wouldn’t be efficient. All parts are welded together.
In this case, I’m making a nuke simulator game, and I’m trying to make it drop.
You could have one root part that’s anchored, and then you could weld every other part in the nuke to that anchored part. Then unanchor it and bombs away
If you have a root part and you’re welding the parts, then just unanchor the root part, otherwise, you can do something like this:
local Model = workspace.Model -- Or where the model is
for _, Part in ipairs(Model:GetDescendants()) do -- ipairs cause it's an array not a dictionary
if Part:IsA("BasePart") then -- Check if it is a part, not a model or something that can't be unanchored
Part.Anchored = false -- All hail the unanchor
end
end
I will say this won’t be as neat as welding it though
So if your nuke is completely welded together, just choose any single part to be the ‘root’ part, and anchor it. Then unanchor it when you want it to drop.
If you un-anchor the parts, they will fly and distort away from the original nuke shape unless they are all welded together.
The strategy above would work, though it’s not a very good way to do it, especially since it isn’t timed and pre-set. The physics of roblox could make this strategy an inefficient way to do it.
Instead use tween service. You could weld all parts in the nuke together using primary part as weld root, and then you could tween the primary part to a specific position, and do your nuke blast event like that.
To weld all parts together with primary part as weld root, you could do:
for _, Part in ipairs(Model:GetDescendants())do
if Part:IsA("BasePart") or Part:IsA("Part") or Part:IsA("MeshPart") and not Part.Name == "NukePrimaryPart" then
local newWeld = Instance.new("WeldConstraint", Model.PrimaryPart)
newWeld.Name = Part.Name
newWeld.Part0 = Model.PrimaryPart
newWeld.Part1 = Part
end
end
Make sure your models PrimaryPart isn’t nil, and is set to a part called “NukePrimaryPart”
Now you can tween your models primary part to the ground, and when the tween finishes you can do your nuke event.
Note: Make sure the code above is in a server script obviously, and make sure you do the tweening and event in a server script.
I have found a fix to the problem. I put the unanchored version of the bomb in ServerStorage, and replaced the anchored one with it. Sorry for wasting your time
Again, I would still use tweenservice instead of un-anchoring the parts.
Thoughhhhh…
I just realized that you could loop through everything in workspace, and when the nuke model or a part inside of it touches any of those parts in workspace…