Help with some code

how do i make it so the block will stop at y = 5

local blockWidth = 5
local blockHeight = 5
local gridWidth = 8
local gridHeight = 20
local blockSpeed = 0.1
local spawnDelay = 3

local function createBlock(scriptEnabled)
	local block = game.ServerStorage.Part:Clone()
	block.Parent = game.Workspace
	block.Anchored = true
	block.CanCollide = true

	local spawnX = math.random(1, gridWidth - 1) * blockWidth
	local spawnY = gridHeight * blockHeight
	local spawnZ = 0
	block.Position = Vector3.new(spawnX, spawnY, spawnZ)

	while true do
		if block.Position.Y - blockHeight/2 <= 0 or block.Parent == nil then
			break
		end
		block.Position = block.Position - Vector3.new(0, blockHeight, 0)
		wait(blockSpeed)

		local nearbyParts = workspace:FindPartsInRegion3(
			Region3.new(block.Position - Vector3.new(blockWidth/2, blockHeight/2, blockWidth/2),
				block.Position + Vector3.new(blockWidth/2, blockHeight/2, blockWidth/2)),
			nil, -- Ignore list
			10 -- MaxParts
		)

		local collided = false
		for _, nearbyPart in pairs(nearbyParts) do
			if nearbyPart ~= block and nearbyPart.CanCollide then
				local topOfNearbyPart = nearbyPart.Position.Y + nearbyPart.Size.Y/2
				local bottomOfBlock = block.Position.Y - blockHeight/2
				local isLandingSpot = math.abs(topOfNearbyPart - bottomOfBlock - 0.05) < 0.01 or math.abs(nearbyPart.Position.X - block.Position.X) < blockWidth/2

				if nearbyPart == game.Workspace.Baseplate or (isLandingSpot and blockHeight <= 10) then
					collided = true
					break
				end
			end
		end

		if collided then
			block.Anchored = true
			break
		end
	end

	-- Create a new block
	createBlock(scriptEnabled)
end

while true do
	createBlock(true) -- pass in true to indicate script is enabled
	wait(spawnDelay)
end
2 Likes

sorry this code

local blockWidth = 10
local blockHeight = 10
local gridWidth = 8
local gridHeight = 20
local blockSpeed = 0.1
local spawnDelay = 3

local function createBlock(scriptEnabled)
	local block = game.ServerStorage.Part:Clone()
	block.Parent = game.Workspace
	block.Anchored = true
	block.CanCollide = true

	local spawnX = math.random(1, gridWidth - 1) * blockWidth
	local spawnY = gridHeight * blockHeight
	local spawnZ = 0
	block.Position = Vector3.new(spawnX, spawnY, spawnZ)

	while true do
		if block.Position.Y - blockHeight/2 <= 0 or block.Parent == nil then
			break
		end

		local stackHeight = 0
		local currentBlock = block
		while currentBlock.Parent ~= nil do
			stackHeight = stackHeight + 1
			currentBlock = currentBlock.Parent
		end

		if stackHeight >= 10 then
			block.Anchored = true
			break
		end

		block.Position = block.Position - Vector3.new(0, blockHeight, 0)
		wait(blockSpeed)

		local nearbyParts = workspace:FindPartsInRegion3(
			Region3.new(block.Position - Vector3.new(blockWidth/2, blockHeight/2, blockWidth/2),
				block.Position + Vector3.new(blockWidth/2, blockHeight/2, blockWidth/2)),
			nil, -- Ignore list
			10 -- MaxParts
		)

		local collided = false
		for _, nearbyPart in pairs(nearbyParts) do
			if nearbyPart ~= block and nearbyPart.CanCollide then
				local topOfNearbyPart = nearbyPart.Position.Y + nearbyPart.Size.Y/2
				local bottomOfBlock = block.Position.Y - blockHeight/2
				local isLandingSpot = math.abs(topOfNearbyPart - bottomOfBlock - 0.05) < 0.01 or math.abs(nearbyPart.Position.X - block.Position.X) < blockWidth/2

				if nearbyPart == game.Workspace.Floor or (isLandingSpot and blockHeight <= 10) then
					collided = true
					break
				end
			end
		end

		if collided then
			block.Anchored = true
			break
		end
	end

	-- Create a new block
	createBlock(scriptEnabled)
end

while true do
	createBlock(true) -- pass in true to indicate script is enabled
	wait(spawnDelay)
end

1 Like

If you’re trying to detect if the Y position is 5, then that won’t always work.

The position is able to have random long decimals, because actual machines aren’t able to create very accurate integers.

Instead, you can check if the Y position is over or below 5, then to set the position to 5 and disable the script:

local x = 0

while task.wait(0.2) do
    
    print(x)
    x += 0.2
    
    if x > 5 then
        print("reached")
        x = 5
    end
    
end

This example script makes the number increase by 0.2 and checks if it goes above 5.

This example script won’t have decimals, but if you deal with physics properties (Like the Y position in your case), then that will have decimals.

2 Likes

You can also just use math.floor (or math.ceil, floor is usually used instead though) to get rid of those long pesky decimals like this:

local decimal_places = 100
-- evil floating point precision error
-- if theres enough 9's in "x", it will start rounding up instead of down
local x = 5.43999999999999999999999999999999999999999
print(math.floor(x * decimal_places) / decimal_places) -- 5.44

That’s a better idea, you can just simply round the value after every given function, before the checks:

math.floor(x)

thenks for you all for helping

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.