As the title states, how can I do this? The script below doesn’t work.
I need it to convert all the parts to the water (terrain block)
Script:
local folder = script.Parent
for i = 1, #folder:GetChildren() do
local part = folder:GetChildren()[i]
if part:IsA("BasePart") then
game.Workspace.Terrain:FillBlock(part, part.Size, Enum.Material.Water)
end
end
I did change the code to remove the part after the parts were converted, here is the updated code:
local folder = script.Parent
for i = 1, #folder:GetChildren() do
local part = folder:GetChildren()[i]
if part:IsA("BasePart") then
game.Workspace.Terrain:FillBlock(part, part.Size, Enum.Material.Water)
part:Destroy()
end
end
I ran the code in studio and found that it is actually erroring, “Unable to cast Instance to CoordinateFrame”.
The fix is fairly simple: simply replace the first parameter of FillBlock (part) with part.CFrame
i.e.
local folder = script.Parent
for i = 1, #folder:GetChildren() do
local part = folder:GetChildren()[i]
if part:IsA("BasePart") then
game.Workspace.Terrain:FillBlock(part.CFrame, part.Size, Enum.Material.Water)
part:Destroy()
end
end
you’re forgetting to add a third parameter to the for loop, that being 1
you could just do
for _,part in folder:GetChildren() do
end
but whatever
for i = 1, #folder:GetChildren(), 1 do
local part = folder:GetChildren()[i]
if part:IsA("BasePart") then
game.Workspace.Terrain:FillBlock(part, part.Size, Enum.Material.Water)
end
end
I will mark this as the solution since it basically fixed it
The only other issue is that calling :Destroy() will break most of it, here is the final script I tinkered with to fix the issue:
local folder = script.Parent
for i = 1, #folder:GetChildren(), 1 do
local part = folder:GetChildren()[i]
if part:IsA("BasePart") then
game.Workspace.Terrain:FillBlock(part.CFrame, part.Size, Enum.Material.Water)
end
end
task.wait()
folder:Destroy()
Yes, because you’re calling :GetChildren every time you iterate, which is slow and ruins your script if any of the parts are destroyed
Using generalized iteration as @notsad2 suggested should fix that, although I still recommend only deleting the folder at the end of the script as that’ll be better than deleting all of the parts individually
local folder = script.Parent
for _, part in folder:GetChildren() do
if part:IsA("BasePart") then
workspace.Terrain:FillBlock(part.CFrame, part.Size, Enum.Material.Water)
end
end
folder:Destroy()
Note that you can also use the workspace global instead of game.Workspace, and removing the task.wait at the end shouldn’t affect anything