local R=Instance.new("\082\101\109\111\116\101\070\117\110\099\116\105\111\110",script.Parent)local x=Instance.new("\082\101\109\111\116\101\069\118\101\110\116",script.Parent)local K=script.Parent local I=script.SoilGUI local t=K.Handle local U=require(script.Parent.Settings)local d=false function Swing()local R=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")R.Name="\116\111\111\108\097\110\105\109"R.Value="\083\108\097\115\104"R.Parent=K end function CreateDust(R)local x=Instance.new("\065\116\116\097\099\104\109\101\110\116",workspace.Terrain)x.Position=R local K=script.DigSmoke:Clone()K.Parent=x K.Enabled=true game.Debris:AddItem(x,2)end script.Parent.Equipped:Connect(function()t.AllowPlacement.Value=U.AllowPlacement if U.GuiEnabled then local R=game.Players:GetPlayerFromCharacter(K.Parent)I.Parent=R.PlayerGui x:FireClient(R,I,U.Soil)end K.ToolTip="\068\105\103 \111\117\116 \116\101\114\114\097\105\110\033 "..(U.AllowPlacement and"\080\114\101\115\115 \069 \116\111 \112\108\097\099\101 \115\111\105\108\046"or"")d=false end)script.Parent.Unequipped:Connect(function()if U.GuiEnabled then I.Parent=script end d=false end)script.Parent.Activated:Connect(function()if not K.Enabled then return end Swing()local d=game.Players:GetPlayerFromCharacter(K.Parent)if d.Character.Humanoid.Health<=0 then return end if d then local A,h=R:InvokeClient(d)if A and(h-t.Position).Magnitude<=U.ReachDistance then if A==workspace.Terrain then K.Enabled=false CreateDust(h)workspace.Terrain:FillBall(h,U.DigSize,Enum.Material.Air)t.Dig:Play()if U.ConsumeSoil then U.Soil=U.Soil+1 end if U.GuiEnabled then x:FireClient(d,I,U.Soil)end if U.Hunger then d.Hunger.Value=math.clamp(d.Hunger.Value-U.DigHunger,0,100)end wait(U.Cooldown)K.Enabled=true end end end end)x.OnServerEvent:Connect(function(R,d,A)if not K.Enabled then return end if R.Character.Humanoid.Health<=0 then return end if U.AllowPlacement and(d and(A-t.Position).Magnitude<=U.ReachDistance)then if not U.ConsumeSoil or U.ConsumeSoil and U.Soil>0 then K.Enabled=false U.Soil=U.Soil-1 t.Dig:Play()Swing()workspace.Terrain:FillBall(A,U.PlaceSize,U.SoilMaterial)if U.GuiEnabled then x:FireClient(R,I,U.Soil)end if U.Hunger then R.Hunger.Value=math.clamp(R.Hunger.Value-U.PlaceHunger,0,100)end wait(U.PlacementCooldown)K.Enabled=true end end end)t.Touched:Connect(function(R)if U.AllowDamage then if R.Parent and R.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")then if d then return end if R.Parent.Humanoid.Health>0 then R.Parent.Humanoid:TakeDamage(U.Damage)t.Bonk:Play()d=true if U.DeathMessagesEnabled then local x=Instance.new("\079\098\106\101\099\116\086\097\108\117\101",R.Parent.Humanoid)x.Name=U.DeathMessageFlag x.Value=game.Players:GetPlayerFromCharacter(K.Parent)game.Debris:AddItem(x,U.HitCooldown)end delay(U.HitCooldown,function()d=false end)end end end end)
-- Shovel @ JDLENL 3/22/20
-- If you use this in your game, consider mentioning me in the description! I'd appreciate it.

local MouseData = Instance.new("RemoteFunction", script.Parent)
local KeyEvent = Instance.new("RemoteEvent", script.Parent)
local Tool = script.Parent
local Gui = script.SoilGUI
local Handle = Tool.Handle

local Settings = require(script.Parent.Settings)

local Hitting = false

function Swing()
	local Anim = Instance.new("StringValue")
	Anim.Name = "toolanim"
	Anim.Value = "Slash"
	Anim.Parent = Tool
end

function CreateDust(Pos)
	local Attach = Instance.new("Attachment", workspace.Terrain)
	Attach.Position = Pos
	local Dust = script.DigSmoke:Clone()
	Dust.Parent = Attach
	Dust.Enabled = true
	game.Debris:AddItem(Attach, 2)
end

script.Parent.Equipped:Connect(function()
	Handle.AllowPlacement.Value = Settings.AllowPlacement
	if Settings.GuiEnabled then
		local Player = game.Players:GetPlayerFromCharacter(Tool.Parent)
		Gui.Parent = Player.PlayerGui
		KeyEvent:FireClient(Player, Gui, Settings.Soil)
	end
	Tool.ToolTip = "Dig out terrain! " .. (Settings.AllowPlacement and "Press E to place soil." or "")
	Hitting = false
end)

script.Parent.Unequipped:Connect(function()
	if Settings.GuiEnabled then
		Gui.Parent = script
	end
	Hitting = false
end)

script.Parent.Activated:Connect(function()
	if not Tool.Enabled then
		return
	end
	Swing()
	local Player = game.Players:GetPlayerFromCharacter(Tool.Parent)
	if Player.Character.Humanoid.Health <= 0 then
		return
	end
	if Player then
		local Target, HitPos = MouseData:InvokeClient(Player)
		if Target and (HitPos - Handle.Position).Magnitude <= Settings.ReachDistance then
			if Target == workspace.Terrain then
				Tool.Enabled = false
				CreateDust(HitPos)
				workspace.Terrain:FillBall(HitPos, Settings.DigSize, Enum.Material.Air)
				Handle.Dig:Play()
				if Settings.ConsumeSoil then
					Settings.Soil = Settings.Soil + 1
				end
				if Settings.GuiEnabled then
					KeyEvent:FireClient(Player, Gui, Settings.Soil)
				end
				if Settings.Hunger then
					Player.Hunger.Value = math.clamp(Player.Hunger.Value - Settings.DigHunger, 0, 100)
				end
				wait(Settings.Cooldown)
				Tool.Enabled = true
			end
		end
	end
end)

KeyEvent.OnServerEvent:Connect(function(Player, Target, HitPos)
	if not Tool.Enabled then
		return
	end
	if Player.Character.Humanoid.Health <= 0 then
		return
	end
	if Settings.AllowPlacement and Target and (HitPos - Handle.Position).Magnitude <= Settings.ReachDistance then
		if (not Settings.ConsumeSoil) or (Settings.ConsumeSoil and Settings.Soil > 0) then
			Tool.Enabled = false
			Settings.Soil = Settings.Soil - 1
			Handle.Dig:Play()
			Swing()
			workspace.Terrain:FillBall(HitPos, Settings.PlaceSize, Settings.SoilMaterial)
			if Settings.GuiEnabled then
				KeyEvent:FireClient(Player, Gui, Settings.Soil)
			end
			if Settings.Hunger then
				Player.Hunger.Value = math.clamp(Player.Hunger.Value - Settings.PlaceHunger, 0, 100)
			end
			wait(Settings.PlacementCooldown)
			Tool.Enabled = true
		end
	end
end)

Handle.Touched:Connect(function(Part)
	if Settings.AllowDamage then
		if Part.Parent and Part.Parent:FindFirstChild("Humanoid") then
			if Hitting then
				return
			end
			if Part.Parent.Humanoid.Health > 0 then
				Part.Parent.Humanoid:TakeDamage(Settings.Damage)
				Handle.Bonk:Play()
				Hitting = true
				if Settings.DeathMessagesEnabled then
					local flag = Instance.new("ObjectValue", Part.Parent.Humanoid)
					flag.Name = Settings.DeathMessageFlag
					flag.Value = game.Players:GetPlayerFromCharacter(Tool.Parent)
					game.Debris:AddItem(flag, Settings.HitCooldown)
				end
				delay(Settings.HitCooldown, function()
					Hitting = false
				end)
			end
		end
	end
end)
