I’m currently working on a basic tree chopping system and I want to know how and what I should make server-sided. I’ll provide my atrocious code so I can explain better. Keep in mind, the whole system is based around the axe. Yes, I know, it’s terrible.
-- Variables
local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local swingAnimation = humanoid:LoadAnimation(tool:WaitForChild("Swing"))
local mouse = player:GetMouse()
-- Debounce variable
local chopping = false
-- Configuration variables
local cooldownTime = 1.5
local distance = 5
-- Main function
tool.Activated:Connect(function()
local target = mouse.Target
if target then
local distanceFromTarget = (humanoidRootPart.Position - target.Position).Magnitude
if distanceFromTarget < distance then
local health = target.Parent:FindFirstChild("Health")
local resourceValue = target.Parent:FindFirstChild("ResourceType")
if resourceValue and resourceValue.Value == "Tree" then
if chopping == false then
chopping = true
print("Tree damaged")
health.Value = health.Value - 1
if health.Value <= 0 then
print("Tree destroyed")
target.Parent:Destroy()
end
swingAnimation:Play()
wait(cooldownTime)
chopping = false
end
end
end
end
end)
Keep in mind that the script works perfectly fine and if there is a better way of doing something than I am (besides half of it needing to be server-sided), please let me know!
Anyway, I know I would have to use RemoteEvents or something like that but I’m still unsure. I would appreciate any help!