How do I make my tree chopping system server-sided?

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!

1 Like

You could probably fire the Remote Event in the Server, from your axe script.

Axe Script:

local tool = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("AxeEvent")

tool.Activated:Connect(function()
  remoteEvent:FireServer()
end)

Upon firing, make the server check if the axe is actually hitting a tree, and do the rest of the code

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("AxeEvent")

local function onTreeHit(--- not sure what to pass in here)
--- Whatever code you use to check if the tree is hit by the axe
end

remoteEvent.OnServerEvent:Connect(onTreeHit)

I believe it could work like this, do keep in mind I’m not that experienced as a scripter, but normally this could keep stuff server-sided. Not really sure if you have to fire it back to the client or not as it uses animations.

But for more info here it’s the link about Remote Events and Functions

1 Like

After an hour or two of programming and researching, I finally got it to work. Here’s how for anyone wondering how to do something similar that are coming across this post late.

Using @djmix224 code sample, I split up my client script into two sections:

The server side will handle

  • Damaging the tree
  • Doing some checks like if the ResourceType and Health values are present.

The client side will handle

  • Debounce
  • Distance checking

If I should still change some stuff around, let me know!!

1 Like