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

What is causing this undesired displacement in parts generated by for loop?

Asked by
AZDev 590 Moderation Voter
7 years ago
Edited 7 years ago

I'm working on a window that when shot, will break. I'm using a for loop to generate the fragments which will fall to the ground. The problem here is that the window fragments, when put together, are bigger than the actual window.

I tested this on a window with a size of [10, 10, 0.2].

My loop divides the window into a 10x10 grid and creates the window with that grid. On a 10x10 size window, the fragments should be 1x1 studs in size and they are. What doesn't make sense to me is how they are actually larger than the window. Here is my code.

local SizeX = workspace.Window1.Size.X
local SizeY = workspace.Window1.Size.Y

for x = -SizeX/2, SizeX/2, SizeX/10 do
    for y = -SizeY/2, SizeY/2, SizeY/10 do
        local Fragment = Instance.new("Part")
        Fragment.Size = Vector3.new(SizeX/10, SizeY/10, 0.2)
        Fragment.Anchored = true
        Fragment.CFrame = workspace.Window1.CFrame * CFrame.new(x, y, Fragment.CFrame.z)
        Fragment.Parent = workspace
    end
end

Screenshot showing what is happening

EDIT:

I divided x and y by 1.111 and it seems to work. I came up with that number by guesswork and would like a real solution.

Here is the edited code:

local SizeX = workspace.Window1.Size.X
local SizeY = workspace.Window1.Size.Y

for x = -SizeX/2, SizeX/2, SizeX/10 do
    for y = -SizeY/2, SizeY/2, SizeY/10 do
        local Fragment = Instance.new("Part")
        Fragment.Size = Vector3.new(SizeX/10, SizeY/10, 0.2) -- Vector3.new(0.5, 0.5, 0)
        Fragment.Anchored = true
        Fragment.CFrame = workspace.Window1.CFrame * CFrame.new(x/1.111, y/1.111, Fragment.CFrame.z+0.2)
        Fragment.Parent = workspace
    end
end
0
Maybe because every loop X increases 1, and same for y, this would eventually lead to a larger area than what you needed joshthegamer456 93 — 5y

Answer this question