Anukun_Lucifer

TreeChopServer Script

Sep 1st, 2025
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.41 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. -- Event handler: เมื่อผู้เล่นฟันต้นไม้
  85. treeChopRequestRemote.OnServerEvent:Connect(function(player, damagePart)
  86.     -- ตรวจสอบ cooldown ของผู้เล่น
  87.     local currentTime = os.clock()
  88.     if playerCooldowns[player] and currentTime - playerCooldowns[player] < CHOP_COOLDOWN then return end
  89.     playerCooldowns[player] = currentTime
  90.  
  91.     -- ตรวจจับว่าฟันโดนต้นไม้หรือไม่
  92.     local hitTreeModel
  93.     for _, treeModel in getTrees() do
  94.         for _, treePart in treeModel:GetChildren() do
  95.             if treePart:IsA("BasePart") and (damagePart.Position - treePart.Position).Magnitude < (damagePart.Size.Magnitude + treePart.Size.Magnitude) / 2 then
  96.                 hitTreeModel = treeModel
  97.                 break
  98.             end
  99.         end
  100.         if hitTreeModel then break end
  101.     end
  102.     if not hitTreeModel or treeWoodCounts[hitTreeModel] == "done" then return end
  103.  
  104.     -- เล่นเสียง HitSound
  105.     local hitSound = hitTreeModel:FindFirstChild("HitSound")
  106.     if hitSound and hitSound:IsA("Sound") then
  107.         hitSound:Play()
  108.     end
  109.  
  110.     -- เอฟเฟกต์สั่นต้นไม้
  111.     for _, treePart in hitTreeModel:GetChildren() do
  112.         if treePart:IsA("BasePart") then
  113.             local originalCFrame = treePart.CFrame
  114.             treePart.CFrame = originalCFrame * CFrame.new(0, 0.5, 0)
  115.             task.wait(0.05)
  116.             treePart.CFrame = originalCFrame
  117.         end
  118.     end
  119.  
  120.     -- ตรวจสอบจำนวนครั้งที่ฟันสูงสุดจาก Tool
  121.     local playerTool = player.Character and player.Character:FindFirstChildOfClass("Tool")
  122.     local slashPerTimeValue = playerTool and playerTool:FindFirstChild("SlashPerTime")
  123.     local maxSlashPerTime = slashPerTimeValue and slashPerTimeValue.Value or 3
  124.  
  125.     -- เพิ่มจำนวนครั้งที่ฟันต้นไม้
  126.     treeHitCounts[hitTreeModel] = (treeHitCounts[hitTreeModel] or 0) + 1
  127.     if treeHitCounts[hitTreeModel] >= maxSlashPerTime then
  128.         -- ต้นไม้ล้ม, spawn ไม้, ลบต้นไม้, respawn
  129.         tweenFall(hitTreeModel)
  130.         spawnWoods(hitTreeModel, hitTreeModel:GetPivot().Position)
  131.         task.wait(1)
  132.         hitTreeModel:Destroy()
  133.         treeHitCounts[hitTreeModel], treeWoodCounts[hitTreeModel] = nil, nil
  134.     end
  135. end)
  136.  
  137.  
Tags: Roblox
Advertisement
Add Comment
Please, Sign In to add comment