Advertisement
beati_kay

axe module script

Mar 10th, 2024
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. --services
  2. local rs = game:GetService("ReplicatedStorage")
  3.  
  4. --modules
  5. local treeInfo = require(script.treeInfo)
  6.  
  7. --variables
  8. local cutThickness = script.variables.cutThickness.Value
  9. local tooClose = script.variables.tooClose.Value
  10.  
  11. --events
  12. local events = rs.events
  13. local cutEvent = events.chop
  14.  
  15.  
  16. local axe = {}
  17.  
  18. --functions
  19. local function Split(target, mouse, cut)
  20.     local top = target.CFrame * CFrame.new(0, target.Size.Y / 2, 0) * CFrame.Angles(math.pi / 2, 0, 0)
  21.     local bottom = target.CFrame * CFrame.new(0, target.Size.Y / -2, 0) * CFrame.Angles(math.pi / -2, 0, 0)
  22.    
  23.     local part0 = Instance.new("Part", target.Parent)
  24.     part0.Size = Vector3.new(target.Size.X, (top.Position - cut.Position).Magnitude, target.Size.Z)
  25.     part0.Color = target.Color
  26.     part0.Material = target.Material
  27.     part0.CFrame = target.CFrame + target.CFrame.UpVector * (top.Position - cut.Position).Magnitude
  28.    
  29.     local part1 = Instance.new("Part", target.Parent)
  30.     part1.Size = Vector3.new(target.Size.X, (bottom.Position - cut.Position).Magnitude, target.Size.Z)
  31.     part1.Color = target.Color
  32.     part1.Material = target.Material
  33.     part1.CFrame = target.CFrame + target.CFrame.UpVector * -(bottom.Position - cut.Position).Magnitude
  34.    
  35.     for i, v in pairs(target:GetChildren()) do
  36.         v:Destroy()
  37.     end
  38.     target:Destroy()
  39. end
  40.  
  41. function PlaceCut(target, mouse)
  42.     local top = target.CFrame * CFrame.new(0, target.Size.Y / 2, 0) * CFrame.Angles(math.pi / 2, 0, 0)
  43.     local bottom = target.CFrame * CFrame.new(0, target.Size.Y / -2, 0) * CFrame.Angles(math.pi / -2, 0, 0)
  44.    
  45.     local cutsFolder = target.cuts
  46.     local cut = Instance.new("Part", cutsFolder)
  47.     cut.Size = Vector3.new(target.Size.X + 0.01, cutThickness, target.Size.Z + 0.01)
  48.     cut.Color = Color3.new(0,0,0)
  49.     cut.CFrame = target.CFrame
  50.     cut.Massless = true
  51.     cut.CanCollide = false
  52.     cut.Name = "cut"
  53.    
  54.     local distance = (target.Position - mouse).magnitude
  55.     if (top.Position-mouse).Magnitude >= (bottom.Position-mouse).Magnitude then
  56.         distance = -distance
  57.     end
  58.     cut.CFrame = target.CFrame + target.CFrame.UpVector * distance
  59.     local weld0 = Instance.new("WeldConstraint", cut)
  60.     weld0.Part0 = cut
  61.     weld0.Part1 = target
  62.    
  63.     task.wait(1)
  64.     Split(target, mouse, cut)
  65. end
  66.  
  67. function CheckCut(target, mouse)
  68.     local top = target.CFrame * CFrame.new(0, target.Size.Y / 2, 0) * CFrame.Angles(math.pi / 2, 0, 0)
  69.     local bottom = target.CFrame * CFrame.new(0, target.Size.Y / -2, 0) * CFrame.Angles(math.pi / -2, 0, 0)
  70.    
  71.     local cutsFolder = target:FindFirstChild("cuts") or Instance.new("Folder", target)
  72.     cutsFolder.Name = "cuts"
  73.     local canCut = true
  74.     for i, v in pairs(cutsFolder:GetChildren()) do
  75.         local distance = (v.Position - mouse).magnitude
  76.         if distance <= tooClose or top.position <= tooClose or bottom.position <= tooClose then
  77.             canCut = false
  78.         end
  79.     end
  80.     if canCut then
  81.         PlaceCut(target, mouse)
  82.     else
  83.         print("too close")
  84.     end
  85. end
  86.  
  87. --connections
  88. cutEvent.OnServerEvent:Connect(function(player, target, mouse)
  89.     CheckCut(target, mouse)
  90. end)
  91.  
  92. return axe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement