I am making a zombie controller script that utilizes module scripts. I have a single zombie controller script that controls the zombie perfectly, but I want to make it so that one script controls all of the zombies in the game, so there is less lag. Recently, I made a topic and @BadDad2004 helped me out and I now have a fully functional script that controls the zombies (Topic: Making a zombie controller for multiple zombies!). The thing is, the code is different from what I wanted and it is a bit buggy. I figured that I could use module scripts to make the zombie controller my way, but I have ran into a problem: .
I looked up this error on the devforum, but there wasn’t a topic like this when I searched it up. The error takes place in my module script which has the functions that control the zombie.
Module:
local module = {}
local collectionservice = game:GetService("CollectionService")
module.findtarget = function(zombie)
local humans = collectionservice:GetTagged("Human")
local zomhumroot = zombie.HumanoidRootPart
local dist = 1000
local target = nil
for i, human in pairs(humans) do
local humanoid = human:FindFirstChild("Humanoid")
local humroot = human:FindFirstChild("HumanoidRootPart")
if humanoid and humroot and human ~= zombie then
if humanoid.Health > 0 then
if (zomhumroot.Position - humroot.Position).magnitude < dist then
dist = (zomhumroot.Position - humroot.Position).magnitude
target = humroot
end
end
end
end
return target
end
module.movementcontrol = function(zombie)
local zomhumroot = zombie.HumanoidRootPart
local zomanoid = zombie.Zombie
local anim = zomanoid.Walk
local anim2 = zomanoid.Attack
local anim3 = zomanoid.Idle
local loadanim = zomanoid:LoadAnimation(anim)
local loadanim2 = zomanoid:LoadAnimation(anim2)
local loadanim3 = zomanoid:LoadAnimation(anim3)
local humroot = module.findtarget
if humroot then
zomanoid:MoveTo(humroot.Position, humroot) --error here
loadanim:Play()
if (zomhumroot.Position - humroot.Position).magnitude < 5 then
loadanim2:Play()
humroot.Parent.Humanoid:TakeDamage(15)
end
else
zomanoid:MoveTo(zomhumroot.Position + Vector3.new(math.random(-50,50),0, math.random(-50,50)), game.Workspace.Part)
loadanim:Stop()
loadanim3:Play()
wait(3)
loadanim3:Stop()
loadanim:Play()
end
end
return module
Main script utilizing the module:
local pathfindingservice = game:GetService("PathfindingService")
local collectionservice = game:GetService("CollectionService")
local serverstorage = game:GetService("ServerStorage")
local zombies = {}
local zombie = serverstorage:WaitForChild("Zombie")
local zombiemodule = require(game.ReplicatedStorage.ZombieModule)
function taghuman(instance)
local human = instance:FindFirstChildWhichIsA("Humanoid")
if human then
collectionservice:AddTag(human, "Human")
end
end
function tagzombie(instance)
local zombie = instance:FindFirstChild("Zombie")
if zombie then
collectionservice:AddTag(zombie, "Zombie")
end
end
game.Workspace.ChildAdded:Connect(taghuman)
print("tagged human")
game.Workspace.ZombieFolder.ChildAdded:Connect(tagzombie)
print("tagged zombie")
while true do
local zombieclone = zombie:Clone()
zombieclone.UpperTorso.CFrame = game.Workspace.Spawners.Spawner.CFrame
zombieclone.Parent = game.Workspace.ZombieFolder
tagzombie(zombieclone)
print("tagged zombie")
zombiemodule.findtarget(zombieclone)
print("Found target for "..zombieclone.Name)
while wait(1) do
zombiemodule.movementcontrol(zombieclone)
print("Moving")
end
wait(5)
end
function initialize()
for i, v in pairs(game:GetChildren()) do
taghuman(v)
print("tagged human")
end
end
initialize()
I do not know what I did wrong. Can you not reference position in a module script?