Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- TreeChopServer
- ระบบฝั่งเซิร์ฟเวอร์สำหรับตัดต้นไม้:
- - รับ event จาก client เมื่อผู้เล่นฟันต้นไม้
- - ตรวจสอบ cooldown, ตรวจจับการฟัน, เล่นเสียง, tween ต้นไม้ล้ม, spawn ไม้, respawn ต้นไม้
- ]]
- -- RemoteEvent สำหรับรับคำขอจาก client
- local treeChopRequestRemote = game:GetService("ReplicatedStorage").TreeChopRequest
- -- อ้างอิง workspace และโฟลเดอร์ที่เกี่ยวข้อง
- local workspace = game:GetService("Workspace")
- local woodsFolder = workspace:FindFirstChild("Woods")
- local treeZoneRespawnFolder = workspace:FindFirstChild("TreeZone_Respawn")
- -- ตัวแปรเก็บจำนวนครั้งที่ฟัน, จำนวนไม้ที่ spawn แล้ว, และ cooldown ของผู้เล่นแต่ละคน
- local treeHitCounts, treeWoodCounts, playerCooldowns = {}, {}, {}
- -- ระยะเวลาคูลดาวน์ระหว่างการฟัน (วินาที)
- local CHOP_COOLDOWN = 0.5
- -- ฟังก์ชันดึง Model ต้นไม้ทั้งหมดในโซน
- local function getTrees()
- local trees = {}
- if treeZoneRespawnFolder then
- for _, object in treeZoneRespawnFolder:GetChildren() do
- if object:IsA("Model") and object.Name == "TREE" then
- trees[#trees+1] = object
- end
- end
- end
- return trees
- end
- -- ฟังก์ชัน tween ต้นไม้ล้ม: หมุน 90 องศาและเลื่อนลงแกน Y -3
- -- เพิ่ม: เล่นเสียง TreeFallSound จาก Model และรอจนเสียงจบก่อนออกจากฟังก์ชัน
- local function tweenFall(treeModel)
- local playedTween = false
- for _, part in treeModel:GetChildren() do
- if part:IsA("BasePart") and not playedTween then
- local tweenService = game:GetService("TweenService")
- -- หมุน 90 องศา และเลื่อนลง Z +10
- local targetCFrame = part.CFrame * CFrame.Angles(math.rad(90),0,0) * CFrame.new(0, 0, 10)
- local tween = tweenService:Create(part, TweenInfo.new(1), {CFrame = targetCFrame})
- tween:Play()
- playedTween = true
- -- หาเสียง TreeFallSound จาก Model
- local treeFallSound = treeModel:FindFirstChild("TreeFallSound")
- if treeFallSound and treeFallSound:IsA("Sound") then
- treeFallSound:Play()
- end
- break
- end
- end
- end
- -- ฟังก์ชัน spawn ไม้เมื่อฟันต้นไม้ครบ
- local function spawnWoods(treeModel, position)
- if treeWoodCounts[treeModel] == "done" then return end
- treeWoodCounts[treeModel] = treeWoodCounts[treeModel] or 0
- local woodsToSpawn = math.min(3 - treeWoodCounts[treeModel], 3)
- if woodsToSpawn <= 0 then
- treeWoodCounts[treeModel] = "done"
- return
- end
- for i = 1, woodsToSpawn do
- local woodPart = Instance.new("Part")
- woodPart.Name = "Wood"
- woodPart.Size = Vector3.new(3.5, 1.5, 1.5)
- woodPart.Shape = Enum.PartType.Cylinder
- woodPart.Position = position + Vector3.new(0, i, 0)
- woodPart.Parent = woodsFolder
- woodPart.Material = Enum.Material.Wood
- woodPart.Color = Color3.fromRGB(167, 92, 26)
- woodPart.Anchored = false
- woodPart.CanCollide = true
- treeWoodCounts[treeModel] = treeWoodCounts[treeModel] + 1
- end
- if treeWoodCounts[treeModel] >= 3 then
- treeWoodCounts[treeModel] = "done"
- end
- end
- -- ฟังก์ชัน respawn ต้นไม้หลังถูกทำลาย
- local function respawnTree(treeModel, zoneFolder)
- local zoneParts = {}
- for _, object in zoneFolder:GetChildren() do
- if object:IsA("BasePart") and object.Name == "TreeZone01" then
- zoneParts[#zoneParts+1] = object
- end
- end
- if #zoneParts == 0 then return end
- local randomZonePart = zoneParts[math.random(1, #zoneParts)]
- local newTreeModel = treeModel:Clone()
- newTreeModel.Parent = zoneFolder
- newTreeModel:PivotTo(randomZonePart.CFrame)
- end
- -- Event handler: เมื่อผู้เล่นฟันต้นไม้
- treeChopRequestRemote.OnServerEvent:Connect(function(player, damagePart)
- -- ตรวจสอบ cooldown ของผู้เล่น
- local currentTime = os.clock()
- if playerCooldowns[player] and currentTime - playerCooldowns[player] < CHOP_COOLDOWN then return end
- playerCooldowns[player] = currentTime
- -- ตรวจจับว่าฟันโดนต้นไม้หรือไม่
- local hitTreeModel
- for _, treeModel in getTrees() do
- for _, treePart in treeModel:GetChildren() do
- if treePart:IsA("BasePart") and (damagePart.Position - treePart.Position).Magnitude < (damagePart.Size.Magnitude + treePart.Size.Magnitude) / 2 then
- hitTreeModel = treeModel
- break
- end
- end
- if hitTreeModel then break end
- end
- if not hitTreeModel or treeWoodCounts[hitTreeModel] == "done" then return end
- -- เล่นเสียง HitSound
- local hitSound = hitTreeModel:FindFirstChild("HitSound")
- if hitSound and hitSound:IsA("Sound") then
- hitSound:Play()
- end
- -- เอฟเฟกต์สั่นต้นไม้
- for _, treePart in hitTreeModel:GetChildren() do
- if treePart:IsA("BasePart") then
- local originalCFrame = treePart.CFrame
- treePart.CFrame = originalCFrame * CFrame.new(0, 0.5, 0)
- task.wait(0.05)
- treePart.CFrame = originalCFrame
- end
- end
- -- ตรวจสอบจำนวนครั้งที่ฟันสูงสุดจาก Tool
- local playerTool = player.Character and player.Character:FindFirstChildOfClass("Tool")
- local slashPerTimeValue = playerTool and playerTool:FindFirstChild("SlashPerTime")
- local maxSlashPerTime = slashPerTimeValue and slashPerTimeValue.Value or 3
- -- เพิ่มจำนวนครั้งที่ฟันต้นไม้
- treeHitCounts[hitTreeModel] = (treeHitCounts[hitTreeModel] or 0) + 1
- if treeHitCounts[hitTreeModel] >= maxSlashPerTime then
- -- ต้นไม้ล้ม, spawn ไม้, ลบต้นไม้, respawn
- tweenFall(hitTreeModel)
- spawnWoods(hitTreeModel, hitTreeModel:GetPivot().Position)
- task.wait(1)
- hitTreeModel:Destroy()
- treeHitCounts[hitTreeModel], treeWoodCounts[hitTreeModel] = nil, nil
- task.spawn(function()
- task.wait(60)
- respawnTree(hitTreeModel, treeZoneRespawnFolder)
- end)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment