How can i make this barrier for mining work

Basically i made a statement so if a block is too far it doesnt get placed ( which later will be obsidian or something unbreakable).

Code:

   if (math.abs((OreCF.Z-Origin.Z)/6) >= (16 + 8))  or (math.abs((OreCF.X-Origin.X)/6) >= (16 + 8))   
    then return end 
 

The problem is it works but its only on some sides as seen on this image:

How can i make it work?

Roblox Luau uses double precision floating point variables for storing numbers.

This means, in Your case, that if the number is not divisible by 6 you will end up with approximate number and not the precise value. This in turn may cause unpredictable (or rather really hard to predict, without inner knowledge of the floating point notation) behaviour in edge cases.

To fix it, You should add an extra leeway to Your condition. Since Your game is block based, the ideal extra should be half of the block size.

Assuming each block has 1 stud width/length, here is the solution:

if (math.abs((OreCF.Z-Origin.Z)/6) >= (16 + 8 + 0.5))  or (math.abs((OreCF.X-Origin.X)/6) >= (16 + 8 + 0.5)) 
1 Like

Thank you for this but this isnt my problem, the problem im having is that on some sides the barrier works and on some dont the. entire place is 16 by 16 and each block is 6x6x6 thats why i divide it by 6
so its basically checking the amount of blocks.

And the game will never have half a block

The formula is correct though, so double check Origin and OreCF.

1 Like

I checked and OreCF and Origin is correct.

After some testing i’ve saw that the blocks on the other side destroy after 24 blocks and not 8

By putting actual numbers to Your formula, You can see for yourself that it does in fact work. So the problem MUST be with either incorrect Origin or OreCF.

As long as Origin is in the middle and OreCF represents actual block it will work as intended.

1 Like

The origin is the first block placed and its Vector3.new(0,0,0)

Can someonee please help me solve this problem?

cant u use math.clamp to limit a number to smth?

1 Like

Nope it would not work, atleast from what i know.

Can someone help me or give me a formula im trying to fix this problem for like an hour now

I also tried doing this but because there can be 8 in 2 diferent places it just cuts the place in half with air blocks

  local X = math.abs(OreCF.X-Origin.X)/6
	local Z = math.abs(OreCF.Z-Origin.Z)/6
if X == (16 + 8)  or Z == (16 + 8) or X == 8 or Z == 8  then return end

Image:

Why you use (16 + 8) instead of 24?

The only thing that can be wrong is incorrect origin or maybe your OreCF. (Also, why you use X-Origin.X and Z-Origin.Z, but not single Vector3 Origin.X and Origin.Z ?)

You guys are trying to solve a problem i don’t have. I have 16 + 8 because later i will implement a variable that i can customize and Origin.X is the X cordinate the first block starts at.

I dont think you guys see what is happening on the image and what the title means. Im trying to make a barrier so every block on the barrier level will become air or later something unbreakable.

Its all because i dont want players to be able to mine their way to the other side of the map

When we look into your code, everything seems fine and working. Issue somewhere else, but not in that 3 lines.

STOP, OreCF.X-Origin.X, is minus?

No from what i can see the formula is everything that is wrong