I’m trying to make a mining game but I can’t really figure out how to fix the problem where it places blocks where it’s not supposed to.
Video:
Code:
local ServerStorage = game:GetService('ServerStorage')
local Folder = workspace.Generated
function round(pos:Vector3)
return Vector3.new(
math.round(pos.X),
math.round(pos.Y),
math.round(pos.Z)
)
end
Folder.ChildRemoved:Connect(function(Block:BasePart)
if not Block.CanMine.Value then return end
local Positions = {
Vector3.new(4,0,0);
Vector3.new(-4,0,0);
Vector3.new(0,4,0);
Vector3.new(0,-4,0);
Vector3.new(0,0,4);
Vector3.new(0,0,-4);
}
for _, Pos in Positions do
local newPos = round(Block.Position + Pos)
local canPlace = true
for _, otherBlock:BasePart in Folder:GetChildren() do
if otherBlock:IsA('BasePart') and round(otherBlock.Position) == newPos then
canPlace = false
break
end
end
for _, Value:Vector3Value in Folder.Values:GetChildren() do
if round(Value.Value) == newPos then
canPlace = false
break
end
end
if canPlace and newPos.Y >= 2 then
local Clone = Block:Clone()
Clone.Position = newPos
Clone.Parent = Folder
if newPos.X < 38 then
Clone.CanMine.Value = false
Clone.Color = Color3.fromRGB(47, 47, 47)
end
end
end
local Drop = Block:Clone()
Drop:ClearAllChildren()
Drop.Position = Block.Position
Drop.Size = Vector3.one
Drop.Anchored = false
Drop.Parent = workspace.Drops
ServerStorage.UpdPhys:Fire(Drop)
local Plr = ServerStorage.UpdPhys.Event:Wait()
local Owner = Instance.new('ObjectValue')
Owner.Value = Plr
Owner.Name = 'Owner'
Owner.Parent = Drop
local Value = Instance.new('Vector3Value')
Value.Value = round(Block.Position)
Value.Parent = Folder.Values
end)
Can anyone help? Thanks.