Is this enemy code good

the enemy fire remote event to the client and the client moves the enemy and then another remote event to the server which does hit detection

server script

local model = script.Parent
local primary = model.PrimaryPart
local waterStream = model:FindFirstChild("waterStream")
local beam = waterStream.Beam
local remoteEvent = model:WaitForChild("RemoteEvent")

local followDuration = 3
local mercyTime = 0.1

local function GetPlayer()
	return game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())]
end

remoteEvent.OnServerEvent:Connect(function(player)
	local connection
	connection = waterStream.Touched:Connect(function(hit)
		if hit.Parent == player.Character then
			hit.Parent.Humanoid.Health = 0
		end
	end)
	task.wait(1.5)
	connection:Disconnect()
end)

task.wait(2 + (10-2)* math.random())

while true do
	task.wait(6)
	local target = GetPlayer()
	if target then
		remoteEvent:FireClient(target, followDuration, mercyTime)
		task.wait(followDuration+mercyTime)
	end
end

local script

local model = script.Parent
local primary = model.PrimaryPart
local waterStream = model:WaitForChild("waterStream")
local beam = waterStream:WaitForChild("Beam")
local RemoteEvent = model:WaitForChild("RemoteEvent")

local runService = game:GetService("RunService")

local player = game.Players.LocalPlayer

RemoteEvent.OnClientEvent:Connect(function(followDuration, mercyTime)
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")
	local HRP = humanoid.RootPart
	local randomDirection
	repeat 
		randomDirection = Vector3.new(math.random(-10, 10), 1, math.random(-10, 10)).Unit
	until randomDirection.Magnitude > 0.1
	local elapsed = 0
	local connection
	connection = runService.PreRender:Connect(function(dt)
		elapsed += dt
		local offset = 10
		local targetPosition = HRP.Position
		model:PivotTo(CFrame.lookAt(targetPosition + Vector3.new(randomDirection.X * offset,0,randomDirection.Z * offset), targetPosition) * CFrame.Angles(0, math.rad(90), 0))
		if elapsed >= followDuration then
			model:PivotTo(CFrame.lookAt(targetPosition + Vector3.new(randomDirection.X * offset,0,randomDirection.Z * offset), targetPosition) * CFrame.Angles(0, math.rad(90), 0))
			connection:Disconnect()
			task.wait(mercyTime)
			RemoteEvent:FireServer()
			beam.Transparency = NumberSequence.new(0)
			task.wait(1.5)
			beam.Transparency = NumberSequence.new(1)
			return
		end
	end)
end)

3 Likes

In my opinion, mercy time is to short. Extend the length so that the player has a better reaction time to what is happening. Another thing to account for is ping, make sure that the player’s ping is accounted for in the script to make it more fair for them as well!

If you have any other questions let me know, thank you!

4 Likes

but do you think this enemy is coded good and does not have any client and server issues?

is this your entire enemy system? even tho it looks goods theres still room for improvement especially in the optimization and organization side.

  1. Consider using Module scripts for your entire enemy system
    that module script should handle everything from when the enemy should attack or should move
  2. Use oop (object - orientated - programming) in creating multiple enemies
  3. never use a loop in remote event (unless your sending less data e.g small tables and small numbers its better to use a networking library to send out less data) sending less data means much more smoother and faster experience. but I still wouldn’t recommend firing remote events on loop
2 Likes