Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- HammerChopClient
- สคริปต์ฝั่ง Client สำหรับควบคุมการฟันต้นไม้ด้วย Hammer:
- - เล่นอนิเมชั่น Hammer
- - เล่นเสียงฟันไม้
- - ส่ง event ไปยัง Server เพื่อแจ้งว่าฟันต้นไม้
- - จัดการ cooldown อนิเมชั่น
- ]]
- -- อ้างอิง Tool (Hammer) ที่เป็น parent ของสคริปต์นี้
- local hammerTool = script.Parent
- -- RemoteEvent สำหรับแจ้ง Server ว่าฟันต้นไม้
- local treeChopRequestRemote = game:GetService("ReplicatedStorage").TreeChopRequest
- -- อ้างอิง LocalPlayer
- local localPlayer = game.Players.LocalPlayer
- -- Part ที่ใช้ตรวจจับการฟัน (DamagePart)
- local damagePart = hammerTool:FindFirstChild("DamagePart")
- -- เสียงฟันไม้
- local slashSound = hammerTool:FindFirstChild("SlashSound")
- -- อนิเมชั่น Hammer
- local hammerAnimation = hammerTool:FindFirstChild("AnimHammer")
- -- ตัวแปรควบคุมสถานะการเล่นอนิเมชั่น
- local isAnimationPlaying = false
- local animationTrack, animationStoppedConnection
- -- ระยะเวลาคูลดาวน์ระหว่างอนิเมชั่น (วินาที)
- local ANIMATION_DELAY = 0.5 -- ปรับระยะห่างระหว่างอนิเมชั่น
- -- ฟังก์ชัน reset สถานะอนิเมชั่น (หยุดอนิเมชั่น, disconnect event)
- local function resetAnimationState()
- isAnimationPlaying = false
- if animationStoppedConnection then
- animationStoppedConnection:Disconnect()
- animationStoppedConnection = nil
- end
- if animationTrack then
- animationTrack:Stop()
- animationTrack = nil
- end
- end
- -- ฟังก์ชันเล่นอนิเมชั่นฟันไม้ + ส่ง event ไป server + เล่นเสียง
- local function playChopAnimation()
- if isAnimationPlaying then return end
- local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
- local humanoid = character:FindFirstChildOfClass("Humanoid")
- if not humanoid then return end
- if hammerAnimation then
- -- โหลดและเล่นอนิเมชั่น Hammer
- animationTrack = humanoid:LoadAnimation(hammerAnimation)
- isAnimationPlaying = true
- animationTrack:Play()
- -- เล่นเสียงฟันไม้
- if slashSound then slashSound:Play() end
- -- แจ้ง Server ว่าฟันไม้
- treeChopRequestRemote:FireServer(damagePart)
- -- เมื่ออนิเมชั่นหยุด รอ cooldown แล้ว reset สถานะ
- animationStoppedConnection = animationTrack.Stopped:Connect(function()
- task.wait(ANIMATION_DELAY)
- resetAnimationState()
- end)
- else
- -- ถ้าไม่มีอนิเมชั่น ให้ส่ง event และเล่นเสียงทันที
- treeChopRequestRemote:FireServer(damagePart)
- if slashSound then slashSound:Play() end
- end
- end
- -- เชื่อม event Activated กับฟังก์ชัน playChopAnimation
- -- เชื่อม event Unequipped กับฟังก์ชัน resetAnimationState
- if hammerTool:IsA("Tool") then
- hammerTool.Activated:Connect(playChopAnimation)
- hammerTool.Unequipped:Connect(resetAnimationState)
- end
Advertisement
Add Comment
Please, Sign In to add comment