I want to create a simple mining system that would be similar to games like azure mines. I am thinking that a good way to do it is when the player mines a block, use raycasting to check if their is a block underneath. If their is no block, generate a new block. Use more raycasting and the same method to generate more blocks on all sides of the newly generated block where the player mined. The problem I found is that if the player mines a block above them, it would generate below the mined block still which can be a problem.

local repStore = game:GetService("ReplicatedStorage")
local utils = require(repStore:WaitForChild("Utils"))
local pick = script.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

pick.Activated:Connect(function()
	local target = mouse.Target
	
	if target and target:FindFirstChild("Ore") then
		-- Generate block underneith if area is open
		local origin = target.Position
		local direction = target.CFrame.UpVector * -1
		local ray = Ray.new(origin, direction * 10)
		local ignoreList = {target}
		local part = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
		local newOre = nil
		
		if (not part) or (not part:FindFirstChild("Ore")) then
			newOre = utils.GenerateOre(target.Position - Vector3.new(0, 4, 0))
		end
		
		-- This part is where the next few rayCasts are based off of
		local rayBase = Instance.new("Part")
		rayBase.Parent = workspace
		rayBase.Transparency = 1
		rayBase.CanCollide = false
		rayBase.Anchored = true
		rayBase.Position = target.Position - Vector3.new(0, 4, 0)
		
		-- Check if right side is empty then gen ore
		origin = rayBase.Position
		direction = rayBase.CFrame.RightVector
		ray = Ray.new(origin, direction * 10)
		ignoreList = {newOre, rayBase}
		part = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
		
		if (not part) or (not part:FindFirstChild("Ore")) then
			utils.GenerateOre(rayBase.Position + Vector3.new(4, 0, 0))
		end
		
		-- Generate left side
		origin = rayBase.Position
		direction = rayBase.CFrame.RightVector * -1
		ray = Ray.new(origin, direction * 10)
		ignoreList = {newOre, rayBase}
		part = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
		
		if (not part) or (not part:FindFirstChild("Ore")) then
			utils.GenerateOre(rayBase.Position + Vector3.new(-4, 0, 0))
		end
		
		-- Do other sides later
		
		target:Destroy()
	end
end)

-- Apply selection box to ore
local selection = Instance.new("SelectionBox")
selection.Color3 = Color3.fromRGB(52, 143, 235)
selection.Parent = player.PlayerGui

local connection
pick.Equipped:Connect(function()
	connection = mouse.Move:Connect(function()
		local target = mouse.Target
		
		if (not target) or (not target:FindFirstChild("Ore")) then
			selection.Adornee = nil
		else
			if target:FindFirstChild("Ore") then
				selection.Adornee = target
			end
		end	
	end)
end)

pick.Unequipped:Connect(function()
	connection:Disconnect() -- Stop mouse.Move event
	selection.Adornee = nil
end)

2 comments

0

Do you have a problem with a script not working and can you post the script? Sorry if I misunderstood your question. It's a bit vague.

0

You could check if the player's magnitude is a certain distance from the block and check if the player is underneath the block.