Hello, I am right now trying to make an entity System for my td: game. I just came to know that MoveTo and pathfindingService are very bad methods for handling movement.
I tried looking for solutions in devorum, but they are too complicated for my small brain to handle
The code below is something i tried, and in the main script I make new enemies with the enemy.new and give the info as needed. I also give the table of mobs with all the info to the client to tween the positions.
I dont know how the server will detect the client’s enemies, Any solutions?
Module script:
local enemy = {}
enemy.__index = enemy
local map = workspace["Crash Test Zone"]
local ps = game:GetService("PhysicsService")
local cs = game:GetService("CollectionService")
local takenIDS = {}
local count = 0
function enemy:Die()
self.HP = 0
end
function enemy.new(info)
local newEnemy = {}
newEnemy.Name = info["Name"]
newEnemy.Speed = info["Speed"]
newEnemy.ID = info["ID"]
newEnemy.CFrame = CFrame.new()
newEnemy.CurrentWaypoint = 1
newEnemy.Health = info["Health"]
newEnemy.MaxHealth = info["Health"]
newEnemy.State = "Moving"
newEnemy.Instance = game.ReplicatedStorage.Enemies:FindFirstChild(info["Name"])
setmetatable(newEnemy, enemy)
return newEnemy
end
return enemy
Summoning enemies script:
local enemy = {}
local mobs = require(script.Parent.Mob)
local takenIDS = {}
local runService = game:GetService("RunService")
local count = 0
local map = game.Workspace["Crash Test Zone"]
local mobss = {}
function generateCode()
local code = math.random(1, 1000000)
-- now we check if it exists
if table.find(takenIDS, code) then
task.wait()
generateCode() -- call the function again
else
table.insert(takenIDS, code)
return code
end
end
function enemy.WalkToWayPoint(thing, timeEachCooldown, position)
if thing.State == "Dead" then return end
if thing.CurrentWaypoint + 1 > #map.Waypoints:GetChildren() then return end
local nextWaypoint = map.Waypoints[thing.CurrentWaypoint + 1]
thing.CFrame = thing.CFrame.lookVector * (thing.Speed * timeEachCooldown)
if (position - nextWaypoint.Position).Magnitude < 1 then
thing.CFrame = CFrame.new(position, nextWaypoint.Position)
thing.CurrentWaypoint += 1
end
end
function enemy.spawnEnemy(enemy, amount)
enemy = string.lower(enemy)
local mob = game.ReplicatedStorage.Enemies:FindFirstChild(enemy)
local code = generateCode()
local info = {
["Name"] = enemy,
["Speed"] = mob:GetAttribute("Speed"),
["Health"] = mob:GetAttribute("Health"),
["ID"] = code
}
for i = 1, amount do
local new = mobs.new(info)
new.CFrame = map.Waypoints:FindFirstChild("1").CFrame
new.CFrame = new.CFrame.Position
table.insert(mobss, new)
end
end
runService.Heartbeat:Connect(function()
count += 1
if count >= 10 then
count = 0
for i,v in pairs(mobss) do
if v.CFrame then
local dist = (v.CFrame - map.Waypoints[v.CurrentWaypoint].Position).Magnitude
enemy.WalkToWayPoint(v, dist/v.Speed, v.CFrame)
else
continue
end
end
game.ReplicatedStorage.Events.Render:FireAllClients(mobss)
end
end)
return enemy
Client:
local ts = game:GetService("TweenService")
function check(mob)
for i,v in pairs(workspace.Enemies:GetChildren()) do
if v:GetAttribute("ID") == mob.ID then
return false
else
continue
end
end
return true
end
game.ReplicatedStorage.Events.Render.OnClientEvent:Connect(function(enemy)
for i,v in pairs(enemy) do
if check(v) then
local clone = v.Instance:Clone()
clone:SetAttribute("ID", v.ID)
clone.Parent = workspace.Enemies
clone:WaitForChild("HumanoidRootPart").CFrame = workspace["Crash Test Zone"].Waypoints[1].CFrame
v.Instance = clone
end
local clone = v.Instance
local goal = {}
goal.Position = workspace["Crash Test Zone"].Waypoints[v.CurrentWaypoint].Position
local distance = (clone:WaitForChild("HumanoidRootPart").Position - workspace["Crash Test Zone"].Waypoints[v.CurrentWaypoint].Position).Magnitude
local times = distance/clone:GetAttribute("Speed")
ts:Create(clone:WaitForChild("HumanoidRootPart"), TweenInfo.new(times), goal)
end
return enemy
end)
Please don’t make fun of my small brain code.