Hello again,
I’m making a tower defense game and i’ve only made enemy pathfinding on the server. I’m still stuck at how to render enemies on client correctly.
I used to render enemies by sending information from the server to the client every 20 heartbeats, however i found this post:
This post shows that i can do it without using a loop to send information. But I still dont understand what to do after reading the post’s solution. (especially when the solution said to workout where the enemy is using the distance)
Here is my code:
Server side [module script] :
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")
local remotes = replicatedStorage:WaitForChild("GameSystem"):WaitForChild("Remotes")
local gFolder = game.Workspace.Game.Game
local waypoints = gFolder.Waypoints
local enemyRenders = gFolder["PositionRenders [Enemy]"]
local IDHandler = require(serverScriptService.ServerResources.Modules.IDHandler)
local Enemy = {}
Enemy.__index = Enemy
function Enemy.new(speed,health,id)
local class = setmetatable({},Enemy)
class.Speed = speed
class.CurrentHealth = health
class.ID = id
class.Died = false
class.Position = waypoints["1"].CFrame
class.CurrentWaypoint = 1
class.CurrentHealth = health
Enemy:Init(class)
return class
end
function Enemy.Destroy(self)
self.Died = true
end
function Enemy:Init(self)
local WPPos : CFrame
--find the enemy value, assign attributes
local cframe = IDHandler:findEnemyWithID(self.ID, enemyRenders)
cframe:SetAttribute("Speed", self.Speed)
cframe:SetAttribute("Health", self.CurrentHealth)
print("set")
--make enemy walk to waypoints
local function moveToWaypoint()
if self.CurrentWaypoint > #waypoints:GetChildren() then IDHandler:removeUniqueID(self.ID) cframe:Destroy() Enemy:Destroy(self) return end
WPPos = waypoints[tostring(self.CurrentWaypoint)].CFrame
cframe.Value = self.Position
local distance = (WPPos - self.Position).Magnitude
local timeWalking = distance / self.Speed
task.wait(timeWalking)
self.Position = WPPos
self.CurrentWaypoint += 1
cframe.Value = self.Position
moveToWaypoint()
end
task.spawn(moveToWaypoint)
end
return Enemy
Client side [local script]:
task.wait(1)
local replicatedStorage = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")
local gameSystem = replicatedStorage:WaitForChild("GameSystem")
local gameFolder = game.Workspace.Game.Game
local modules = gameSystem.Modules
local remotes = gameSystem.Remotes
local assets = gameSystem.Assets
local MobData = require(modules.EnemiesAndTowers.Enemies.MobData)
local hpDisplay = assets.VisualEffects.HealthDisplay
gameFolder["PositionRenders [Enemy]"].ChildAdded:Connect(function(Child : CFrameValue)
local mob : Model = MobData:RetrieveMobKey(Child.Name, "Model"):Clone()
mob.Parent = gameFolder.Enemies
local hp = Child:GetAttribute("Health")
local speed = Child:GetAttribute("Speed")
local old_pos = Child.Value
Child.Changed:Connect(function(value)
local distance = (value - old_pos).Magnitude
local timeWalking = distance / speed
tweenService:Create(mob.PrimaryPart,TweenInfo.new(timeWalking, Enum.EasingStyle.Linear), {CFrame = CFrame.new(value)}):Play()
old_pos = value
end)
end)
If anyone atleast explain to me what does it mean by “workout where the enemy is using the distance”, or knows what I’m missing, what I’m doing wrong , or if I should revert back to the heartbeat loop method, please let me know, and I’ll try my best to fix it.