Anukun_Lucifer

TreeChopServer Script

Aug 31st, 2025
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.13 KB | Source Code | 0 0
  1. --[[
  2.     TreeChopServer
  3.     ระบบฝั่งเซิร์ฟเวอร์สำหรับตัดต้นไม้:
  4.     - รับ event จาก client เมื่อผู้เล่นฟันต้นไม้
  5.     - ตรวจสอบ cooldown, ตรวจจับการฟัน, เล่นเสียง, tween ต้นไม้ล้ม, spawn ไม้, respawn ต้นไม้
  6. ]]
  7.  
  8. -- RemoteEvent สำหรับรับคำขอจาก client
  9. local treeChopRequestRemote = game:GetService("ReplicatedStorage").TreeChopRequest
  10.  
  11. -- อ้างอิง workspace และโฟลเดอร์ที่เกี่ยวข้อง
  12. local workspace = game:GetService("Workspace")
  13. local woodsFolder = workspace:FindFirstChild("Woods")
  14. local treeZoneRespawnFolder = workspace:FindFirstChild("TreeZone_Respawn")
  15.  
  16. -- ตัวแปรเก็บจำนวนครั้งที่ฟัน, จำนวนไม้ที่ spawn แล้ว, และ cooldown ของผู้เล่นแต่ละคน
  17. local treeHitCounts, treeWoodCounts, playerCooldowns = {}, {}, {}
  18.  
  19. -- ระยะเวลาคูลดาวน์ระหว่างการฟัน (วินาที)
  20. local CHOP_COOLDOWN = 0.5
  21.  
  22. -- ฟังก์ชันดึง Model ต้นไม้ทั้งหมดในโซน
  23. local function getTrees()
  24.     local trees = {}
  25.     if treeZoneRespawnFolder then
  26.         for _, object in treeZoneRespawnFolder:GetChildren() do
  27.             if object:IsA("Model") and object.Name == "TREE" then
  28.                 trees[#trees+1] = object
  29.             end
  30.         end
  31.     end
  32.     return trees
  33. end
  34.  
  35. -- ฟังก์ชัน tween ต้นไม้ล้ม: หมุน 90 องศาและเลื่อนลงแกน Y -3
  36. -- เพิ่ม: เล่นเสียง TreeFallSound จาก Model และรอจนเสียงจบก่อนออกจากฟังก์ชัน
  37. local function tweenFall(treeModel)
  38.     local playedTween = false
  39.     for _, part in treeModel:GetChildren() do
  40.         if part:IsA("BasePart") and not playedTween then
  41.             local tweenService = game:GetService("TweenService")
  42.             -- หมุน 90 องศา และเลื่อนลง Z +10
  43.             local targetCFrame = part.CFrame * CFrame.Angles(math.rad(90),0,0) * CFrame.new(0, 0, 10)
  44.             local tween = tweenService:Create(part, TweenInfo.new(1), {CFrame = targetCFrame})
  45.             tween:Play()
  46.             playedTween = true
  47.             -- หาเสียง TreeFallSound จาก Model
  48.             local treeFallSound = treeModel:FindFirstChild("TreeFallSound")
  49.             if treeFallSound and treeFallSound:IsA("Sound") then
  50.                 treeFallSound:Play()
  51.             end
  52.             break
  53.         end
  54.     end
  55. end
  56.  
  57. -- ฟังก์ชัน spawn ไม้เมื่อฟันต้นไม้ครบ
  58. local function spawnWoods(treeModel, position)
  59.     if treeWoodCounts[treeModel] == "done" then return end
  60.     treeWoodCounts[treeModel] = treeWoodCounts[treeModel] or 0
  61.     local woodsToSpawn = math.min(3 - treeWoodCounts[treeModel], 3)
  62.     if woodsToSpawn <= 0 then
  63.         treeWoodCounts[treeModel] = "done"
  64.         return
  65.     end
  66.     for i = 1, woodsToSpawn do
  67.         local woodPart = Instance.new("Part")
  68.         woodPart.Name = "Wood"
  69.         woodPart.Size = Vector3.new(3.5, 1.5, 1.5)
  70.         woodPart.Shape = Enum.PartType.Cylinder
  71.         woodPart.Position = position + Vector3.new(0, i, 0)
  72.         woodPart.Parent = woodsFolder
  73.         woodPart.Material = Enum.Material.Wood
  74.         woodPart.Color = Color3.fromRGB(167, 92, 26)
  75.         woodPart.Anchored = false
  76.         woodPart.CanCollide = true
  77.         treeWoodCounts[treeModel] = treeWoodCounts[treeModel] + 1
  78.     end
  79.     if treeWoodCounts[treeModel] >= 3 then
  80.         treeWoodCounts[treeModel] = "done"
  81.     end
  82. end
  83.  
  84. -- ฟังก์ชัน respawn ต้นไม้หลังถูกทำลาย
  85. local function respawnTree(treeModel, zoneFolder)
  86.     local zoneParts = {}
  87.     for _, object in zoneFolder:GetChildren() do
  88.         if object:IsA("BasePart") and object.Name == "TreeZone01" then
  89.             zoneParts[#zoneParts+1] = object
  90.         end
  91.     end
  92.     if #zoneParts == 0 then return end
  93.     local randomZonePart = zoneParts[math.random(1, #zoneParts)]
  94.     local newTreeModel = treeModel:Clone()
  95.     newTreeModel.Parent = zoneFolder
  96.     newTreeModel:PivotTo(randomZonePart.CFrame)
  97. end
  98.  
  99. -- Event handler: เมื่อผู้เล่นฟันต้นไม้
  100. treeChopRequestRemote.OnServerEvent:Connect(function(player, damagePart)
  101.     -- ตรวจสอบ cooldown ของผู้เล่น
  102.     local currentTime = os.clock()
  103.     if playerCooldowns[player] and currentTime - playerCooldowns[player] < CHOP_COOLDOWN then return end
  104.     playerCooldowns[player] = currentTime
  105.  
  106.     -- ตรวจจับว่าฟันโดนต้นไม้หรือไม่
  107.     local hitTreeModel
  108.     for _, treeModel in getTrees() do
  109.         for _, treePart in treeModel:GetChildren() do
  110.             if treePart:IsA("BasePart") and (damagePart.Position - treePart.Position).Magnitude < (damagePart.Size.Magnitude + treePart.Size.Magnitude) / 2 then
  111.                 hitTreeModel = treeModel
  112.                 break
  113.             end
  114.         end
  115.         if hitTreeModel then break end
  116.     end
  117.     if not hitTreeModel or treeWoodCounts[hitTreeModel] == "done" then return end
  118.  
  119.     -- เล่นเสียง HitSound
  120.     local hitSound = hitTreeModel:FindFirstChild("HitSound")
  121.     if hitSound and hitSound:IsA("Sound") then
  122.         hitSound:Play()
  123.     end
  124.  
  125.     -- เอฟเฟกต์สั่นต้นไม้
  126.     for _, treePart in hitTreeModel:GetChildren() do
  127.         if treePart:IsA("BasePart") then
  128.             local originalCFrame = treePart.CFrame
  129.             treePart.CFrame = originalCFrame * CFrame.new(0, 0.5, 0)
  130.             task.wait(0.05)
  131.             treePart.CFrame = originalCFrame
  132.         end
  133.     end
  134.  
  135.     -- ตรวจสอบจำนวนครั้งที่ฟันสูงสุดจาก Tool
  136.     local playerTool = player.Character and player.Character:FindFirstChildOfClass("Tool")
  137.     local slashPerTimeValue = playerTool and playerTool:FindFirstChild("SlashPerTime")
  138.     local maxSlashPerTime = slashPerTimeValue and slashPerTimeValue.Value or 3
  139.  
  140.     -- เพิ่มจำนวนครั้งที่ฟันต้นไม้
  141.     treeHitCounts[hitTreeModel] = (treeHitCounts[hitTreeModel] or 0) + 1
  142.     if treeHitCounts[hitTreeModel] >= maxSlashPerTime then
  143.         -- ต้นไม้ล้ม, spawn ไม้, ลบต้นไม้, respawn
  144.         tweenFall(hitTreeModel)
  145.         spawnWoods(hitTreeModel, hitTreeModel:GetPivot().Position)
  146.         task.wait(1)
  147.         hitTreeModel:Destroy()
  148.         treeHitCounts[hitTreeModel], treeWoodCounts[hitTreeModel] = nil, nil
  149.         task.spawn(function()
  150.             task.wait(60)
  151.             respawnTree(hitTreeModel, treeZoneRespawnFolder)
  152.         end)
  153.     end
  154. end)
Tags: Roblox lua
Advertisement
Add Comment
Please, Sign In to add comment