Shape wont spawn in correct position

When I loop through the code to spawn the 3 shapes in they spawn like 100 studs away or something

Code:

local DisplayedShapes = script.Parent:WaitForChild("DisplayedShapes")

local Shapes = game:GetService("ReplicatedStorage"):WaitForChild("Shapes")

local ShapeOrder = {
	"Square";
	"Ball";
	"Triangle";
};

local ShapeSpawnPositions = script.Parent:WaitForChild("ShapeSpawnPositions")

local function LoopedShapeSpinner()
	
	while true do
		
		for _, DisplayedShape in pairs(DisplayedShapes:GetChildren()) do
			
			if DisplayedShape then
				
				if DisplayedShape.Name ~= "Ball" then
					
					DisplayedShape.CFrame = DisplayedShape.CFrame * CFrame.Angles(0,-0.04375,0)
					
				end
				
			end
			
		end
		
		task.wait(0.025)
		
	end
	
end

spawn(LoopedShapeSpinner)

while true do
	
	for i = 1,#ShapeOrder,1 do
		
		if #DisplayedShapes:GetChildren() == 3 then DisplayedShapes:ClearAllChildren() end
		
		for num = 1,3,1 do
			
			local ClonedShape = Shapes:FindFirstChild(ShapeOrder[i]):Clone()
			ClonedShape.Parent = DisplayedShapes
			
			ClonedShape.Anchored = true
			
			ClonedShape.CanCollide = true
			
			ClonedShape.Size = Vector3.new(2.5,2.5,2.5)
			
			for _, ShapeSpawn in pairs(ShapeSpawnPositions:GetChildren()) do
				
				ShapeSpawn.Taken.SpawnTaken.Value = true

				ClonedShape.CFrame = CFrame.new(ShapeSpawn.Position)

				print(ClonedShape.Position)

				print(ShapeSpawn.Position)
				
			end
			
		end
		
		task.wait(4)
		
		for _, ShapeSpawn in pairs(ShapeSpawnPositions:GetChildren()) do
			
			ShapeSpawn.Taken.SpawnTaken.Value = false
			
		end
		
	end
	
	task.wait()
	
end
1 Like

Im bad at programming, but try this code (maybe help it to you):

Code

local DisplayedShapes = script.Parent:WaitForChild("DisplayedShapes")
local Shapes = game:GetService("ReplicatedStorage"):WaitForChild("Shapes")

local ShapeOrder = {
    "Square",
    "Ball",
    "Triangle",
}

local ShapeSpawnPositions = script.Parent:WaitForChild("ShapeSpawnPositions")

local function LoopedShapeSpinner()
    while true do
        for _, DisplayedShape in pairs(DisplayedShapes:GetChildren()) do
            if DisplayedShape and DisplayedShape.Name ~= "Ball" then
                DisplayedShape.CFrame = DisplayedShape.CFrame * CFrame.Angles(0, -0.04375, 0)
            end
        end
        task.wait(0.025)
    end
end

spawn(LoopedShapeSpinner)

while true do
    if #DisplayedShapes:GetChildren() == 3 then
        DisplayedShapes:ClearAllChildren()
    end

    for i = 1, #ShapeOrder, 1 do
        local ClonedShape = Shapes:FindFirstChild(ShapeOrder[i]):Clone()
        ClonedShape.Parent = DisplayedShapes
        ClonedShape.Anchored = true
        ClonedShape.CanCollide = true
        ClonedShape.Size = Vector3.new(2.5, 2.5, 2.5)

        -- Assign each shape to a specific spawn position
        local ShapeSpawn = ShapeSpawnPositions:FindFirstChild(tostring(i))

        if ShapeSpawn then
            ShapeSpawn.Taken.SpawnTaken.Value = true
            ClonedShape.CFrame = CFrame.new(ShapeSpawn.Position)
            print(ClonedShape.Position)
            print(ShapeSpawn.Position)
        end
    end

    task.wait(4)

    for _, ShapeSpawn in pairs(ShapeSpawnPositions:GetChildren()) do
        ShapeSpawn.Taken.SpawnTaken.Value = false
    end
end

The problem is not abt assigning but that the shapes spawn at the wrong position. All 3 shapes are the same and then after 4 seconds they all 3 change to another shape which is the same

Try assigning each shape its own spawn position with separate name attributes.

Try this
Code:

local DisplayedShapes = script.Parent:WaitForChild("DisplayedShapes")
local Shapes = game:GetService("ReplicatedStorage"):WaitForChild("Shapes")

local ShapeOrder = {
    "Square";
    "Ball";
    "Triangle";
}

local ShapeSpawnPositions = script.Parent:WaitForChild("ShapeSpawnPositions")

local function LoopedShapeSpinner()
    while true do
        for _, DisplayedShape in pairs(DisplayedShapes:GetChildren()) do
            if DisplayedShape then
                if DisplayedShape.Name ~= "Ball" then
                    DisplayedShape.CFrame = DisplayedShape.CFrame * CFrame.Angles(0, -0.04375, 0)
                end
            end
        end
        task.wait(0.025)
    end
end

spawn(LoopedShapeSpinner)

while true do
    if #DisplayedShapes:GetChildren() == 3 then
        DisplayedShapes:ClearAllChildren()
    end

    for i = 1, #ShapeOrder do
        local ClonedShape = Shapes:FindFirstChild(ShapeOrder[i]):Clone()
        ClonedShape.Parent = DisplayedShapes
        ClonedShape.Anchored = true
        ClonedShape.CanCollide = true
        ClonedShape.Size = Vector3.new(2.5, 2.5, 2.5)

        local ShapeSpawn = ShapeSpawnPositions:GetChildren()[i]
        ClonedShape.CFrame = CFrame.new(ShapeSpawn.Position)

        print("ClonedShape Position: ", ClonedShape.Position)
        print("ShapeSpawn Position: ", ShapeSpawn.Position)
    end

    task.wait(4)

    for _, ShapeSpawn in pairs(ShapeSpawnPositions:GetChildren()) do
        ShapeSpawn.Taken.SpawnTaken.Value = false
    end
end