--local mob = require(script.Mobs)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local stat = require(script.Statistics)
local mob = {}
local castle = workspace.Castle
local healthVal = castle.PlayerBase.Health
function mob.Move(mob, map)
local nodes = castle.Nodes
local node = nodes.start
local Speed = stat.Data[mob.Name]["Speed"]
local MaxHealth = stat.Data[mob.Name]["MaxHealth"]
local speed = 2
local rotSpeed = .1
for w=1, #nodes:GetChildren() do
local node = nodes:FindFirstChild(w)
if node then
-- rotate enemy
local rotateTween = TweenService:Create(mob, TweenInfo.new(rotSpeed, Enum.EasingStyle.Circular), {CFrame = CFrame.new(mob.Position, node.Position)})
rotateTween:Play()
rotateTween.Completed:Wait()
-- move enemy
local distance = (mob.Position - node.Position).Magnitude
local moveTween = TweenService:Create(mob, TweenInfo.new(distance / speed / Speed, Enum.EasingStyle.Linear), {Position = node.Position})
moveTween:Play()
moveTween.Completed:Wait()
end
end
healthVal.Value -= MaxHealth
mob:Destroy()
end
function mob.SpawnEnemy (name, map)
local enemy = ReplicatedStorage.Enemies:GetChildren()
local entity = enemy[math.random(1, #enemy)]:Clone()
entity.CFrame = castle.Nodes.start.CFrame
entity.Parent = workspace.Enemies
coroutine.wrap(mob.Move)(entity)
end
while healthVal.Value > 0 do
task.wait(1)
mob.SpawnEnemy("Enemy1", castle)
end
if healthVal.Value <= 0 then
print("Game Over!")
local e = game.Workspace.Enemies:GetChildren()
-- destroy all the children
for i = 1, #e do
e[i]:Destroy()
end
end
return mob
#How would I turn this into a modulescript?
18 messages · Page 1 of 1 (latest)
so this is currently running in the main script inside serverscriptservice, but I want to convert the functions into a modulescript so I can call them in the main script efficiently
Copy paste into a module script and you're good to go. 😄
I see you already have a table and its elements and you're returning it.
I don't see the issue.
Just let those parts that are not dependent on the table outside of the module script.
For example while healthVal.Value > 0
That you might access by turning your table into a class.
And instancing a new mob into the script by accessing the module script.
Then accessing its health.
script:
--local mob = require(script.Mobs)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local stat = require(script.Statistics)
local mob = require(script.MobModule).SpawnEnemy(...)--insert arguments here
--from here on replace the various health stats with the mobs by accessing mob.health or whatever it is.
local castle = workspace.Castle
local healthVal = castle.PlayerBase.Health
while healthVal.Value > 0 do
task.wait(1)
mob.SpawnEnemy("Enemy1", castle)
end
if healthVal.Value <= 0 then
print("Game Over!")
local e = game.Workspace.Enemies:GetChildren()
-- destroy all the children
for i = 1, #e do
e[i]:Destroy()
end
end
module script:
local mob = {}
mob.__index = mob;
function mob.Move(mob, map)
local nodes = castle.Nodes
local node = nodes.start
local Speed = stat.Data[mob.Name]["Speed"]
local MaxHealth = stat.Data[mob.Name]["MaxHealth"]
local speed = 2
local rotSpeed = .1
for w=1, #nodes:GetChildren() do
local node = nodes:FindFirstChild(w)
if node then
-- rotate enemy
local rotateTween = TweenService:Create(mob, TweenInfo.new(rotSpeed, Enum.EasingStyle.Circular), {CFrame = CFrame.new(mob.Position, node.Position)})
rotateTween:Play()
rotateTween.Completed:Wait()
-- move enemy
local distance = (mob.Position - node.Position).Magnitude
local moveTween = TweenService:Create(mob, TweenInfo.new(distance / speed / Speed, Enum.EasingStyle.Linear), {Position = node.Position})
moveTween:Play()
moveTween.Completed:Wait()
end
end
healthVal.Value -= MaxHealth
mob:Destroy()
end
function mob.SpawnEnemy (name, map)
local enemy = ReplicatedStorage.Enemies:GetChildren()
local entity = enemy[math.random(1, #enemy)]:Clone()
entity.CFrame = castle.Nodes.start.CFrame
entity.Parent = workspace.Enemies
coroutine.wrap(mob.Move)(entity)
end
return mob
Now, I haven't checked if everything's right.
You have to check that the module doesn't have cross-references to the script.
And you gotta replace, inside of the script, all the (now) wrong references. For example the health, that is now to refer as to a mob property from the module script.
So after properly instantiating the mob you'll look for his health property with mob.health or smth like that.
Hello, sorry for the late response but when I try to replace "castle" in castle.Nodes with "map" it says the map is nil?
** You are now Level 9! **
that was an error I had earlier when testing it in the normal script