Hello there,
I’m Nick I am a programmer working on a tower defense game. I have a decent system that works and can functions, however the moving feature for the entities does not work. I have no idea what I have done wrong or how I can fix it properly.
I am using 1 server script and 2 module scripts to run this (I will put the codes in a sec)
Can somebody please help me fix my issues and get them working?
Server:
-->> Services <<--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
-->> Folders <<--
local Events = ReplicatedStorage.Events
local Modules = ServerStorage.Modules
-->> Modules <<--
local GM_H = require(Modules.GameModes_Handler)
local EntityHandler = require(Modules.Entity)
-->> Functions <<--
local function playersInServer()
return #game.Players:GetPlayers()
end
-->> Configuration <<--
local Minimum_HP = 0
local Current_HP = 250
local Max_HP = 250
local Minimum_Shield = 0
local Current_Shield = 250
local Max_Shield = 250
local Enemy_HP_Multiplier -- Unknown because there could be 1 - ∞ in a server
local Enemy_Speed_Multiplier -- Unknown because there could be 1 - ∞ in a server
local Max_Placement -- Unknown because there could be 1 - ∞ in a server
---EntityHandler.Spawn("Default", 10, 3, Enemy_HP_Multiplier, Enemy_Speed_Multiplier)
GM_H.Begin("Classic")
-->> Loops <<--
spawn(function()
while true do
wait(0.25)
print("There are "..playersInServer().." in the server.")
Max_Placement = playersInServer() * 30
Enemy_Speed_Multiplier = playersInServer()
Enemy_HP_Multiplier = playersInServer()
print("The new placement for towers are:", Max_Placement)
end
end)
Entity:
local Entity = {}
Entity.Move = function(NewClone, map)
local Humanoid = NewClone:FindFirstChild("Humanoid")
print("Humanoid object:", Humanoid)
local Map = map
assert(Map and Map.Waypoints, "Map or Waypoints not found")
local Waypoints = Map.Waypoints
local maxDistance = 0.15
local reachedWaypoint = false
for waypoint = 1, #Waypoints:GetChildren() do
reachedWaypoint = false
print("Moving to waypoint", waypoint)
print("Waypoint position:", Waypoints[waypoint].Position)
print("Current position:", NewClone.HumanoidRootPart.Position)
NewClone.Humanoid:MoveTo(Waypoints[waypoint].Position)
while not reachedWaypoint do
local distance = (NewClone.HumanoidRootPart.Position - Waypoints[waypoint].Position).Magnitude
if distance <= maxDistance then
reachedWaypoint = true
print("Reached waypoint", waypoint)
end
wait(0.1)
end
end
end
Entity.Spawn = function(requested: string, quantity: number, cooldown: number, healthMultiplier: number, speedMultiplier: number, map)
local doesExist = game:GetService("ReplicatedStorage").Entities:FindFirstChild(requested)
if not doesExist then warn("Requested does not exist:", requested) return false end
for i = 1, quantity do
local success, err = pcall(function()
local NewCopy = doesExist:Clone()
NewCopy.Parent = workspace.Mobs
NewCopy.HumanoidRootPart.CFrame = game.Workspace.Map.Design.Begin.CFrame
NewCopy.Humanoid.WalkSpeed = NewCopy.Humanoid.WalkSpeed * speedMultiplier
NewCopy.Humanoid.MaxHealth = NewCopy.Humanoid.MaxHealth * healthMultiplier
NewCopy.Humanoid.Health = NewCopy.Humanoid.Health * healthMultiplier
coroutine.wrap(Entity.Move)(NewCopy, map)
end)
if err then
warn("There was an error:", err)
end
wait(cooldown)
end
end
return Entity
Round
local Spawner = require(game.ServerStorage.Modules.Entity)
local Handler = {}
Handler.Classic = {
{
Message = "Round 1 - BEGIN!",
Enemies = {
{
Name = "Default",
Quantity = 10,
Cooldown = 3,
},
{
Name = "Default",
Quantity = 10,
Cooldown = 3,
}
},
Time_Interval = 5,
},
{
Message = "Round 2 - BEGIN!",
Enemies = {
{
Name = "Default",
Quantity = 10,
Cooldown = 3,
},
{
Name = "Default",
Quantity = 10,
Cooldown = 3,
}
},
Time_Interval = 5,
}
}
Handler.Begin = function(Selected: string)
if Selected == "Classic" then
for _, round in ipairs(Handler.Classic) do
print(round.Message)
for _, enemy in ipairs(round.Enemies) do
local entityName = enemy.Name
local quantity = enemy.Quantity
local cooldown = enemy.Cooldown
Spawner.Spawn(entityName, quantity, cooldown, 0, 0, game.Workspace.Map)
end
wait(round.Time_Interval)
end
end
end
return Handler