Block stacking game not working

Hello :lobster: ,

I am trying to make a game where you have to stack the blocks :star: . The problem is that I can’t seem to get the system working.

Every time :clock2: the blocks get too small, they start to behave weirdly.

Example Media

Videos:

https://gyazo.com/32c52ff43c8045a2be5f487c3e62a711

Images:

Full Code

ATTENTION: messy code ahead

local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Track
camera.CameraSubject = workspace.CamTracker

local function endGame()
	
	local paused = true
	error("Game Over!")
	
end

local function spawnDropBlockX(sizeX: number, pos: Vector3)

	local dropBlock = Instance.new("Part")
	dropBlock.Color = currentBlock.Color
	dropBlock.Name = "TempBlock"

	dropBlock.Position = pos
	dropBlock.Size = Vector3.new(sizeX, 1, previousBlock.Size.Z)

	dropBlock.Anchored = false
	dropBlock.Parent = workspace

	Debris:AddItem(dropBlock, .25)

end

local function splitBlockOnX(hangOver: number)
	
	local direction
	if hangOver > 0 then

		direction = 1

	else

		direction = -1

	end

	local newXSize = previousBlock.Size.X - math.abs(hangOver)
	local newXPosition = previousBlock.Position.X + (hangOver / 2)

	currentBlock.Size = Vector3.new(newXSize, currentBlock.Size.Y, currentBlock.Size.Z)
	currentBlock.Position = Vector3.new(newXPosition, currentBlock.Position.Y, currentBlock.Position.Z)

	local fallingBlockSize = currentBlock.Size.X - newXSize

	local blockEdge = currentBlock.Position.X + (newXSize / 2 * direction)
	local fallingBlockXPos = blockEdge + fallingBlockSize / 2 * direction
	
	if hangOver ~= 0 then
		
		spawnDropBlockX(fallingBlockSize, Vector3.new(fallingBlockXPos, currentBlock.Position.Y, currentBlock.Position.Z))
		
	end
	
	previousBlock = currentBlock
	currentScore += 1

end

local function spawnDropBlockZ(sizeZ: number, pos: Vector3)
	
	local dropBlock = Instance.new("Part")
	dropBlock.Color = currentBlock.Color
	dropBlock.Name = "TempBlock"

	dropBlock.Position = pos
	dropBlock.Size = Vector3.new(previousBlock.Size.X, 1, sizeZ)
	
	dropBlock.Material = Enum.Material.SmoothPlastic

	dropBlock.Anchored = false
	dropBlock.Parent = workspace

	Debris:AddItem(dropBlock, .25)
	
end

local function splitBlockOnZ(hangOver: number)
	
	local direction
	if hangOver > 0 then

		direction = 1

	else

		direction = -1

	end

	local newZSize = previousBlock.Size.Z - math.abs(hangOver)
	local newZPosition = previousBlock.Position.Z + (hangOver / 2)

	currentBlock.Size = Vector3.new(currentBlock.Size.X, currentBlock.Size.Y, newZSize)
	currentBlock.Position = Vector3.new(currentBlock.Position.X, currentBlock.Position.Y, newZPosition)

	local fallingBlockSize = currentBlock.Size.Z - newZSize

	local blockEdge = currentBlock.Position.Z + (newZSize / 2 * direction)
	local fallingBlockZPos = blockEdge + fallingBlockSize / 2 * direction
	
	if hangOver ~= 0 then
		
		spawnDropBlockZ(fallingBlockSize, Vector3.new(currentBlock.Position.X, currentBlock.Position.Y, fallingBlockZPos))
		
	end
	
	previousBlock = currentBlock
	currentScore += 1
	
end

local function activated()

	paused = true
	currentTween:Pause()

	local goal = {}
	goal.Position = Vector3.new(0, workspace.CamTracker.Position.Y + 1, 0)

	local info = TweenInfo.new(

		.35,
		Enum.EasingStyle.Circular,
		Enum.EasingDirection.InOut,
		0

	)

	local tween = TweenService:Create(workspace.CamTracker, info, goal)
	tween:Play()
	
	if moveDirection == "X" then
		
		local hangOver = currentBlock.Position.X - previousBlock.Position.X
		
		if (hangOver < 0.25 and hangOver > -0.25) then

			hangOver = 0

		end
		
		if hangOver >= currentBlock.Size.X then
			
			endGame()
			
		else
			
			splitBlockOnX(hangOver)
			
		end
		
	else
		
		local hangOver = currentBlock.Position.Z - previousBlock.Position.Z
		
		if (hangOver < 0.25 and hangOver > -0.25) then

			hangOver = 0

		end
		
		if hangOver >= currentBlock.Size.Z then

			endGame()
			
		else
			
			splitBlockOnZ(hangOver)

		end
		
	end
	
	task.wait()	
	paused = false

end

button.MouseButton1Click:Connect(activated)

local function spawnX()
	
	moveDirection = "X"
	
	local newPosition = Vector3.new(10, (0.5 + currentScore), previousBlock.Position.Z)
	local newPart = part:Clone()

	currentBlock = newPart
	local previousColor = previousBlock.Color

	local targetColor = Color3.fromRGB(0, 0, 0)
	newPart.Color = previousColor:Lerp(targetColor, 1)

	newPart.Anchored = true
	newPart.BrickColor = BrickColor.random()

	newPart.Transparency = 0
	newPart.Position = newPosition

	newPart.Size = Vector3.new(previousBlock.Size.X, 1, previousBlock.Size.Z)
	newPart.Parent = workspace

	if paused then repeat task.wait() until not paused end

	local goal = {}
	goal.Position = Vector3.new(-10, newPart.Position.Y, newPart.Position.Z)
	
	local info = TweenInfo.new(
		
		2,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.InOut,
		-1,
		true,
		.1
		
	)
	
	local tween = TweenService:Create(newPart, info, goal)
	currentTween = tween
	
	tween:Play()
	
	if not paused then repeat task.wait() until paused end
	
end

local function spawnZ()
	
	moveDirection = "Z"
	
	local newPosition = Vector3.new(previousBlock.Position.X, (0.5 + currentScore), 10)
	local newPart = part:Clone()

	currentBlock = newPart
	local previousColor = previousBlock.Color

	local targetColor = Color3.fromRGB(0, 0, 0)
	newPart.Color = previousColor:Lerp(targetColor, 1)

	newPart.Anchored = true
	newPart.BrickColor = BrickColor.random()

	newPart.Transparency = 0
	newPart.Position = newPosition

	newPart.Size = Vector3.new(previousBlock.Size.X, 1, previousBlock.Size.Z)
	newPart.Parent = workspace

	if paused then repeat task.wait() until not paused end
	
	local goal = {}
	goal.Position = Vector3.new(newPart.Position.X, newPart.Position.Y, -10)

	local info = TweenInfo.new(

		2,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.InOut,
		-1,
		true,
		.1

	)

	local tween = TweenService:Create(newPart, info, goal)
	tween:Play()
	
	currentTween = tween
	
	if not paused then repeat task.wait() until paused end
	
end

while task.wait() do

	if currentScore % 2 == 0 then
		
		spawnX()
		
	else
		
		spawnZ()
		
	end

end

Okay, so I figured it out :open_mouth: with the help of a tutorial meant for another game engine.

Here’s the video if anyone needs: