Digging system delayed

What is this SYSTEM and how it’s work?

This is a shovel system. by holding a mouse for specific duration then fired remote to server to perform a digging by Terrain:FillBall() with Enum.Material.Air

  • What is problem?*

There is no problem at first time digging. But for second time. a raycast is cast too early before a terrain got dig. Which made digging inaccurate.

Local Script

--!nocheck
local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local TS = game:GetService("TweenService")

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart: Part = Character:WaitForChild("HumanoidRootPart")
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")

local Head = Character:WaitForChild("Head")
local UpperTorso = Character:WaitForChild("UpperTorso")
local Waist: Motor6D = UpperTorso:WaitForChild("Waist")

local DEFAULT_WAIST = Waist.C0

local Dig = RS:WaitForChild("Dig")

local CurrentCamera = workspace.CurrentCamera
local Terrian = workspace.Terrain

local Tool = script.Parent

local Equipped = false
local Holding = false

local Radius = 5
local Duration = 1
local Range = 20

-- threads
local thread
local tweening

-- connections
local Connection = nil

local SphereHandleAdornment = Instance.new("SphereHandleAdornment")
SphereHandleAdornment.Adornee = workspace
SphereHandleAdornment.Transparency = 0.75
SphereHandleAdornment.AlwaysOnTop = true
SphereHandleAdornment.Radius = Radius * 0.5

local color = {
	idle = Color3.fromRGB(13, 105, 172),
	action = Color3.new(1, 0, 0)
}

local params = RaycastParams.new()
params.FilterDescendantsInstances = {Character}

local tweeninfo = TweenInfo.new(Duration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)

local function GetMouse()
	local mouseposition = UIS:GetMouseLocation()
	local pointtoray = CurrentCamera:ViewportPointToRay(mouseposition.X, mouseposition.Y)

	return workspace:Raycast(pointtoray.Origin, pointtoray.Direction * 1000, params)
end

Tool.Equipped:Connect(function()
	Equipped = true
	
	SphereHandleAdornment.Color3 = color.idle
	
	Connection = RunService.RenderStepped:Connect(function()
		local ray = GetMouse()
		
		if not ray then return end
		local position = ray.Position
		
		local Distance = vector.magnitude(HumanoidRootPart.Position - position)
		if Distance > Range then
			if not Holding then 
				SphereHandleAdornment.Parent = nil
			end
		else
			SphereHandleAdornment.Parent = workspace
			if not Holding then
				SphereHandleAdornment.CFrame = CFrame.new(position)
			end
		end
	end)
end)

Tool.Unequipped:Connect(function()
	Equipped = false
	
	SphereHandleAdornment.Parent = nil
	
	if Connection then
		Connection:Disconnect()
		Connection = nil
	end
end)

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if not Equipped and gameProcessedEvent then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if Holding then return end
		
		Holding = true
		
		thread = task.defer(function()
			while Holding and Equipped do
				local previous_walkspeed =  Humanoid.WalkSpeed
				local previous_jumpheight = Humanoid.JumpHeight
				
				local ray = GetMouse()
				if not ray then 	
					RunService.RenderStepped:Wait()
					continue
				end
				
				local instance = ray.Instance
				local position = ray.Position
				SphereHandleAdornment.CFrame = CFrame.new(position)
				
				local Distance = vector.magnitude(HumanoidRootPart.Position - position)
				if Distance > Range then
					RunService.RenderStepped:Wait()
					
					Humanoid.WalkSpeed = previous_walkspeed
					Humanoid.JumpHeight = previous_jumpheight
					Humanoid.AutoRotate = true
					continue
				end					
				
				local material = ray.Material
								
				if not instance:IsA("Terrain") then 
					RunService.RenderStepped:Wait()
					
					Humanoid.WalkSpeed = previous_walkspeed
					Humanoid.JumpHeight = previous_jumpheight
					Humanoid.AutoRotate = true
					continue 
				end
				
				--print("digging")
								
				Humanoid.WalkSpeed = 0
				Humanoid.JumpHeight = 0
				Humanoid.AutoRotate = false
				
				HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position, vector.create(position.X, HumanoidRootPart.Position.Y, position.Z))
				
				local dir = UpperTorso.Position - position
				Waist.C0 *= CFrame.Angles(-math.atan2(dir.Y, vector.magnitude(vector.create(dir.X, 0, dir.Z))), 0, 0)
					
				tweening = TS:Create(SphereHandleAdornment, tweeninfo, {Color3 = color.action})
				tweening:Play()

				task.wait(Duration)

				Dig:FireServer(position, instance, material, Radius, Range)
				--warn("finish")
				Waist.C0 = DEFAULT_WAIST
				
				Humanoid.WalkSpeed = previous_walkspeed
				Humanoid.JumpHeight = previous_jumpheight
				Humanoid.AutoRotate = true
				
				SphereHandleAdornment.Color3 = color.idle	
				
				--task.wait(1) is just for test
			end
		end)
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if not Equipped and gameProcessedEvent then return end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		Holding = false
		
		Waist.C0 = DEFAULT_WAIST
		
		Humanoid.WalkSpeed = 16
		Humanoid.JumpHeight = 7.2
		Humanoid.AutoRotate = true
		
		SphereHandleAdornment.Color3 = color.idle
		
		if thread then
			task.cancel(thread)
			thread = nil
			--warn("canceled")
		end
		
		if tweening then
			tweening:Cancel()
			tweening = nil
		end
	end
end)

Server Script

local RS = game:GetService("ReplicatedStorage")

local Terrain = workspace.Terrain

RS.Dig.OnServerEvent:Connect(function(player: Player, position: vector, instance: Instance?, material: MaterialVariant, radius: number, range)
	if not position then return end
	if not (instance and instance:IsA("Terrain")) then return end
	if instance.Material == Enum.Material.Water then return end
	
	local Character = player.Character or player.CharacterAdded:Wait()
	local HumanoidRootPart = Character.HumanoidRootPart
	
	local Distance = vector.magnitude(HumanoidRootPart.Position - position)
	if Distance > range then return end
	
	Terrain:FillBall(position, radius, Enum.Material.Air)
	
	RS.Dig:FireAllClients(position, material, radius)	
end)

1. Start holding mouse (Ball is indicator, Color blue is idle color. when player start holding it’s change to Red by tweenservice)


2. Finished but still holding
As you can see a red ball’s position is not at the ground


3. Unholded
now the ball is at correct position

Video

I don’t know if you are still having this issue but I think the delay issue might come from the timing between terrain removal FillBall and when the next raycast happens

you could try adding a short wait or check after the server applies the dig before allowing the next raycast. Something like after Terrain:FillBall(position, radius, Enum.Material.Air) wait a few frames or until terrain is updated before firing the next raycast

Also make sure the client updates correctly maybe use workspace.Terrain:ReadVoxels() or some confirmation that the terrain changed before you cast again

That way you ensure the terrain is actually dug before doing another raycast which should fix the inaccurate digging on subsequent attempts

That’s interesting. :sweat_smile: But i have zero understanding with workspace.Terrain:ReadVoxels(). Can you provide me more or give me an example. Thank you for your kindness.

Hi Bacon! Here’s the documentation on ReadVoxels if you hadn’t been able to find that.
https://create.roblox.com/docs/reference/engine/classes/Terrain#ReadVoxels

Long story short @Suvry1 is right that you should be using ReadVoxels() as it lets you check what the terrain actually looks like on the client by scanning a region and then returning the material and how “filled” (occupied) each voxel is. That means after digging, you can wait until those voxels become mostly empty before doing another raycast. Should help make the digging feel immediate instead of delayed.

As per documentation it’ll return 2 3D arrays.
1 is going to be Enum.Material (https://create.roblox.com/docs/reference/engine/enums/Material) and a Size Field.

2 is going to be occupancy values, where 0 is air and 1 is fully solid. Then anything in between is sort of slopes etc and a Size Field.

Have a play around with it :slight_smile: