Anukun_Lucifer

HammerChopClient Script

Aug 31st, 2025
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.76 KB | Source Code | 0 0
  1. --[[
  2.     HammerChopClient
  3.     สคริปต์ฝั่ง Client สำหรับควบคุมการฟันต้นไม้ด้วย Hammer:
  4.     - เล่นอนิเมชั่น Hammer
  5.     - เล่นเสียงฟันไม้
  6.     - ส่ง event ไปยัง Server เพื่อแจ้งว่าฟันต้นไม้
  7.     - จัดการ cooldown อนิเมชั่น
  8. ]]
  9.  
  10. -- อ้างอิง Tool (Hammer) ที่เป็น parent ของสคริปต์นี้
  11. local hammerTool = script.Parent
  12.  
  13. -- RemoteEvent สำหรับแจ้ง Server ว่าฟันต้นไม้
  14. local treeChopRequestRemote = game:GetService("ReplicatedStorage").TreeChopRequest
  15.  
  16. -- อ้างอิง LocalPlayer
  17. local localPlayer = game.Players.LocalPlayer
  18.  
  19. -- Part ที่ใช้ตรวจจับการฟัน (DamagePart)
  20. local damagePart = hammerTool:FindFirstChild("DamagePart")
  21.  
  22. -- เสียงฟันไม้
  23. local slashSound = hammerTool:FindFirstChild("SlashSound")
  24.  
  25. -- อนิเมชั่น Hammer
  26. local hammerAnimation = hammerTool:FindFirstChild("AnimHammer")
  27.  
  28. -- ตัวแปรควบคุมสถานะการเล่นอนิเมชั่น
  29. local isAnimationPlaying = false
  30. local animationTrack, animationStoppedConnection
  31.  
  32. -- ระยะเวลาคูลดาวน์ระหว่างอนิเมชั่น (วินาที)
  33. local ANIMATION_DELAY = 0.5 -- ปรับระยะห่างระหว่างอนิเมชั่น
  34.  
  35. -- ฟังก์ชัน reset สถานะอนิเมชั่น (หยุดอนิเมชั่น, disconnect event)
  36. local function resetAnimationState()
  37.     isAnimationPlaying = false
  38.     if animationStoppedConnection then
  39.         animationStoppedConnection:Disconnect()
  40.         animationStoppedConnection = nil
  41.     end
  42.     if animationTrack then
  43.         animationTrack:Stop()
  44.         animationTrack = nil
  45.     end
  46. end
  47.  
  48. -- ฟังก์ชันเล่นอนิเมชั่นฟันไม้ + ส่ง event ไป server + เล่นเสียง
  49. local function playChopAnimation()
  50.     if isAnimationPlaying then return end
  51.     local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
  52.     local humanoid = character:FindFirstChildOfClass("Humanoid")
  53.     if not humanoid then return end
  54.     if hammerAnimation then
  55.         -- โหลดและเล่นอนิเมชั่น Hammer
  56.         animationTrack = humanoid:LoadAnimation(hammerAnimation)
  57.         isAnimationPlaying = true
  58.         animationTrack:Play()
  59.         -- เล่นเสียงฟันไม้
  60.         if slashSound then slashSound:Play() end
  61.         -- แจ้ง Server ว่าฟันไม้
  62.         treeChopRequestRemote:FireServer(damagePart)
  63.         -- เมื่ออนิเมชั่นหยุด รอ cooldown แล้ว reset สถานะ
  64.         animationStoppedConnection = animationTrack.Stopped:Connect(function()
  65.             task.wait(ANIMATION_DELAY)
  66.             resetAnimationState()
  67.         end)
  68.     else
  69.         -- ถ้าไม่มีอนิเมชั่น ให้ส่ง event และเล่นเสียงทันที
  70.         treeChopRequestRemote:FireServer(damagePart)
  71.         if slashSound then slashSound:Play() end
  72.     end
  73. end
  74.  
  75. -- เชื่อม event Activated กับฟังก์ชัน playChopAnimation
  76. -- เชื่อม event Unequipped กับฟังก์ชัน resetAnimationState
  77. if hammerTool:IsA("Tool") then
  78.     hammerTool.Activated:Connect(playChopAnimation)
  79.     hammerTool.Unequipped:Connect(resetAnimationState)
  80. end
Tags: Roblox lua
Advertisement
Add Comment
Please, Sign In to add comment