Jenga Block placements kinda

basically i have this jenga like game but instead of random parts I have models instead :sunglasses:

and not all blocks are the same so I already have a random block picker function

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(1.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.In)
local StartingPos = CFrame.new(-140, 778, 251) -- X + 14

-- the blocks are 42 long 14 wide and 14 tall
local BlockHeight = 8

-- gets blocks
local BlockFolder = game.ServerStorage.BlockTypes
-- tables for the block picker thing
local Blocks = {
	["Climbable"] = BlockFolder.Climbable,
	["HND"] = BlockFolder.HND,
	["HallWay"] = BlockFolder.HallWay,
--	["Hollow"] = BlockFolder.Hollow,
	["OWL"] = BlockFolder.OWL,
	["OWR"] = BlockFolder.OWR,
	["OneWay"] = BlockFolder.OneWay,
	["SafeRoom"] = BlockFolder.SafeRoom,
	["Solid"] = BlockFolder.Solid,
	["TrapRoom"] = BlockFolder.TrapRoom
}
local Rarity = {
	["Climbable"] = 15, -- 10
	["HND"] = 10,
	["HallWay"] = 12, --7
--	["Hollow"] = 15,
	["OWL"] = 10,
	["OWR"] = 10,
	["OneWay"] = 13,
	["SafeRoom"] = 21, --16
	["Solid"] = 4,
	["TrapRoom"] = 15
}
--block picker function using rarities
local function GetRandomByRarity()

	
	local NumRandom =  math.random(1, 100)
	local counter = 0
	for Item, weight in pairs(Rarity) do
		counter += weight
		if NumRandom <= counter then
			return Blocks[Item]
		end
	end

end

local function Generation()
	local LoopedTimes = 0
	
	repeat
		local Block = GetRandomByRarity():Clone()
		Block.Parent = workspace
		Block:PivotTo(StartingPos * CFrame.new(14, 75, 0))
		--local Tween = TweenService:Create(Block, Info, {Position = StartingPos * Vector3.new(LoopedTimes * -14, 0, 0)})
		--Tween:Play()
        -- the tween doesnt work lmao
		LoopedTimes += 1
		wait(1.5)

	until LoopedTimes >= (BlockHeight * 3)
	
	
end
-- just to see if the generation works
delay(2, Generation)

The start position is at the side of the platform. If you have ever played blocks n props I’m trying to achieve the same thing

The problem is that I don’t know what to do and the generation just sucks so can anyone help?

2 Likes

?? im confused on ur problem

aaaaaaaaaaaaaaaaa word lim

1 Like
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(1.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.In)
local StartingPos = CFrame.new(-140, 778, 251) -- X + 14

-- the blocks are 42 long 14 wide and 14 tall
local BlockHeight = 8

-- gets blocks
local BlockFolder = game.ServerStorage.BlockTypes
-- tables for the block picker thing
local Blocks = {
	["Climbable"] = BlockFolder.Climbable,
	["HND"] = BlockFolder.HND,
	["HallWay"] = BlockFolder.HallWay,
--	["Hollow"] = BlockFolder.Hollow,
	["OWL"] = BlockFolder.OWL,
	["OWR"] = BlockFolder.OWR,
	["OneWay"] = BlockFolder.OneWay,
	["SafeRoom"] = BlockFolder.SafeRoom,
	["Solid"] = BlockFolder.Solid,
	["TrapRoom"] = BlockFolder.TrapRoom
}
local Rarity = {
	["Climbable"] = 15, -- 10
	["HND"] = 10,
	["HallWay"] = 12, --7
--	["Hollow"] = 15,
	["OWL"] = 10,
	["OWR"] = 10,
	["OneWay"] = 13,
	["SafeRoom"] = 21, --16
	["Solid"] = 4,
	["TrapRoom"] = 15
}
--block picker function using rarities
local function GetRandomByRarity()

	
	local NumRandom =  math.random(1, 100)
	local counter = 0
	for Item, weight in pairs(Rarity) do
		counter += weight
		if NumRandom <= counter then
			return Blocks[Item]
		end
	end

end
local offsets = {-14,0,14}
local function Generation()
    for h = 0,BlockHeight-1 do
	    for r = 1,3 do
		    local newBlock = GetRandomByRarity():Clone()
		    newBlock.Parent = workspace
		    local targetFrame = CFrame.new(StartingPos+Vector3.new(0,14*h,0))
		    if h%2 == 1 then
			    targetFrame *= CFrame.Angles(0,math.pi/2,0)
		    end
		    targetFrame *= CFrame.new(0,0,offsets[r])
		    newBlock:PivotTo(targetFrame)
	    end
    end
end
-- just to see if the generation works
delay(2, Generation)

you will need to change the line
targetFrame *= CFrame.new(0,0,offsets[r])
to
targetFrame *= CFrame.new(offsets[r],0,0)
if your jenga block model is long on the X axis.

StartingPos needs to be the bottom/middle of where you want the tower to generate

1 Like

Could this be an issue on how my blocks are oriented? And also because that the primary parts for a lot of these are also different as well?

If the primary part of the blocks is different the script wont work because it relies on the primary part being in a constant relative CFrame for each jenga block

1 Like

I See also I accidentally sent this To the wrong post mb lol

this worked very well except for some generation the whole tower would fall, but a consistent problem is that all blocks were placed sideways, ALL OF THEM, why is this?

i also fixed all of the primary parts to be the same center part and stuff

All the blocks are placed sideways because of the orientation of the primary parts of all the block models. You could fix this by rotating the targetFrame by 90 degrees on the correct axis. Since I dont know the CFrame of the primary part im not sure which axis you would need to rotate on and whether you need to rotate by +90 or -90 degrees, but you can test that easily.
Add this before pivoting the block:

targetFrame *= CFrame.Angles(0,0,math.pi/2)
and change where you put the math.pi/2 until it works

1 Like

well great! we are so close to being done lol. I just have problems with two of my models which are just cloned and put in there original position before being placed away in a folder in server storage. they have everything welded right and there primary parts are perfectly fine. so what could be some reasons to this

btw this is script so far:

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(1.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.In)
local StartingPos = Vector3.new(-154, 778, 265) -- X + 14

-- the blocks are 42 long 14 wide and 14 tall
local BlockHeight = 8

-- gets blocks
local BlockFolder = game.ServerStorage.BlockTypes
-- tables for the block picker thing
local Blocks = {
	["Climbable"] = BlockFolder.Climbable,
	["HND"] = BlockFolder.HND,
	["HallWay"] = BlockFolder.HallWay,
	--	["Hollow"] = BlockFolder.Hollow,
	["OWL"] = BlockFolder.OWL,
	["OWR"] = BlockFolder.OWR,
	["OneWay"] = BlockFolder.OneWay,
	["SafeRoom"] = BlockFolder.SafeRoom,
	["Solid"] = BlockFolder.Solid,
	["TrapRoom"] = BlockFolder.TrapRoom
}
local Rarity = {
	["Climbable"] = 15, -- 10
	["HND"] = 10,
	["HallWay"] = 12, --7
	--	["Hollow"] = 15,
	["OWL"] = 10,
	["OWR"] = 10,
	["OneWay"] = 13,
	["SafeRoom"] = 21, --16
	["Solid"] = 4,
	["TrapRoom"] = 15
}
--block picker function using rarities
local function GetRandomByRarity()


	local NumRandom =  math.random(1, 100)
	local counter = 0
	for Item, weight in pairs(Rarity) do
		counter += weight
		if NumRandom <= counter then
			return Blocks[Item]
		end
	end

end
local offsets = {-14,0,14}
local function Generation()
	for h = 0,BlockHeight-1 do
		for r = 1,3 do
			local newBlock = GetRandomByRarity():Clone()
			newBlock.Parent = workspace.NewBlocks
			newBlock.Region.Anchored = true
			local targetFrame = CFrame.new(StartingPos+Vector3.new(0,14*h,0))
			if h%2 == 1 then
				targetFrame *= CFrame.Angles(0,math.pi/2,0)
			end
			targetFrame *= CFrame.new(0,0,offsets[r])
			targetFrame *= CFrame.Angles(-math.pi/2,0,0)
                        -- perhaps the pivot to is not working???
			newBlock:PivotTo(targetFrame)
		end
	end
	for _, newBlock in pairs(workspace.NewBlocks:GetChildren()) do
		newBlock.Region.Anchored = false

	end
end
-- just to see if the generation works
delay(2, Generation)