I'm working on a project at the moment that includes trees and other objects that can be destroyed by using specific tools.
The tools simply run an animation when activated.
Tools - local script
local Tool = script.Parent local character = game.Players.LocalPlayer.Character local CanSwing = true local Hit = script.Parent.Hitting ------------------ Tool.Activated:Connect(function() if CanSwing == true and Hit.Value == false then CanSwing = false game.ReplicatedStorage["Tool swing"]:FireServer("1952121150") wait(0.3) Hit.Value = true wait(0.4) CanSwing = true Hit.Value = false end end)
I'm running a Touched function on the objects, that checks to see if the tool is hitting when making contact. This runs fine locally, but of coarse, this doesn't work as intended on servers.
Objects - script
local Root = script.Parent.Parent local TreeModel = script.Parent local tree = script.Parent.TreeCentre local HitSound = game.Workspace.Sounds.TreeHit --local HitSound = script.Parent.TreeHit local Hits = 0 local HitsN = 20 --This is the health of the tree. local CanHit = true TreeModel.PrimaryPart = TreeModel.TreeCentre debounce = false ------------------Functions------------------ function OnStart() wait() tree.Anchored = true tree.CanCollide = true TreeModel:MakeJoints() local centre = TreeModel for _,part in pairs(TreeModel:GetDescendants()) do if part:IsA("BasePart") and part ~= TreeModel.PrimaryPart then local weld = Instance.new("Weld") weld.Part0 = TreeModel.PrimaryPart weld.Part1 = part weld.C1 = part.CFrame:toObjectSpace(TreeModel.PrimaryPart.CFrame) weld.Parent = TreeModel.PrimaryPart end end end OnStart() tree.Touched:Connect(function(hit) if debounce == true then return end --Delays the function a bit. debounce = true if hit.Parent:findFirstChild("ToolHead")and hit.Parent:findFirstChild("Hitting")then if CanHit == true and hit.Parent.Hitting.Value == true then --you are actually hitting, and not just bumping into the tree CanHit = false LeavesDrop() HitSound:play() wait() TreeModel:SetPrimaryPartCFrame(TreeModel:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(4), 0)) wait() TreeModel:SetPrimaryPartCFrame(TreeModel:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(-4), 0)) wait() TreeModel:SetPrimaryPartCFrame(TreeModel:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(-4), 0)) wait() TreeModel:SetPrimaryPartCFrame(TreeModel:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(4), 0)) Hits = Hits + hit.Parent.hitLvl.Value if Hits >= HitsN then Hits = 0 TreeDestroy() else wait(0.6) --If the tree isn't dead, then wait, CanHit = true --and make the tree hittable again. end LeavesDrop() end end debounce = false end) function LeavesDrop() for i,v in pairs(script.Parent:GetDescendants()) do if v:IsA("ParticleEmitter") then if v.Enabled == false then v.Enabled = true else v.Enabled = false end end end end function TreeDestroy() TreeModel.Chopped.Value = true tree.Anchored = false LeavesDrop() end
I did some testing, and apparently the objects won't identify the value of hit.Parent.Hitting.Value.
How can i get this to work on servers? I've thought about using RemoteEvents but i'm not sure how to integrate this into the scripts.
I managed to hook the objects and the tool up with a RemoteEvent
, and so far it works.
I still don't get your comment, mattchew1010. Sorry