local w=Instance.new("\082\101\109\111\116\101\070\117\110\099\116\105\111\110",script.Parent)local k=Instance.new("\082\101\109\111\116\101\069\118\101\110\116",script.Parent)local u=script.Parent local X=script.SoilGUI local a=u.Handle local y=require(script.Parent.Settings)local l=false function Swing()local w=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")w.Name="\116\111\111\108\097\110\105\109"w.Value="\083\108\097\115\104"w.Parent=u end function CreateDust(w)local k=Instance.new("\065\116\116\097\099\104\109\101\110\116",workspace.Terrain)k.Position=w local u=script.DigSmoke:Clone()u.Parent=k u.Enabled=true game.Debris:AddItem(k,2)end script.Parent.Equipped:Connect(function()a.AllowPlacement.Value=y.AllowPlacement if y.GuiEnabled then local w=game.Players:GetPlayerFromCharacter(u.Parent)X.Parent=w.PlayerGui k:FireClient(w,X,y.Soil)end u.ToolTip="\068\105\103 \111\117\116 \116\101\114\114\097\105\110\033 "..(y.AllowPlacement and"\080\114\101\115\115 \069 \116\111 \112\108\097\099\101 \115\111\105\108\046"or"")l=false end)script.Parent.Unequipped:Connect(function()if y.GuiEnabled then X.Parent=script end l=false end)script.Parent.Activated:Connect(function()if not u.Enabled then return end Swing()local l=game.Players:GetPlayerFromCharacter(u.Parent)if l.Character.Humanoid.Health<=0 then return end if l then local U,z=w:InvokeClient(l)if U and(z-a.Position).Magnitude<=y.ReachDistance then if U==workspace.Terrain then u.Enabled=false CreateDust(z)workspace.Terrain:FillBall(z,y.DigSize,Enum.Material.Air)a.Dig:Play()if y.ConsumeSoil then y.Soil=y.Soil+1 end if y.GuiEnabled then k:FireClient(l,X,y.Soil)end if y.Hunger then l.Hunger.Value=math.clamp(l.Hunger.Value-y.DigHunger,0,100)end wait(y.Cooldown)u.Enabled=true end end end end)k.OnServerEvent:Connect(function(w,l,U)if not u.Enabled then return end if w.Character.Humanoid.Health<=0 then return end if y.AllowPlacement and(l and(U-a.Position).Magnitude<=y.ReachDistance)then if not y.ConsumeSoil or y.ConsumeSoil and y.Soil>0 then u.Enabled=false y.Soil=y.Soil-1 a.Dig:Play()Swing()workspace.Terrain:FillBall(U,y.PlaceSize,y.SoilMaterial)if y.GuiEnabled then k:FireClient(w,X,y.Soil)end if y.Hunger then w.Hunger.Value=math.clamp(w.Hunger.Value-y.PlaceHunger,0,100)end wait(y.PlacementCooldown)u.Enabled=true end end end)a.Touched:Connect(function(w)if y.AllowDamage then if w.Parent and w.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")then if l then return end if w.Parent.Humanoid.Health>0 then w.Parent.Humanoid:TakeDamage(y.Damage)a.Bonk:Play()l=true if y.DeathMessagesEnabled then local k=Instance.new("\079\098\106\101\099\116\086\097\108\117\101",w.Parent.Humanoid)k.Name=y.DeathMessageFlag k.Value=game.Players:GetPlayerFromCharacter(u.Parent)game.Debris:AddItem(k,y.HitCooldown)end delay(y.HitCooldown,function()l=false end)end end end end)



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)
