Script that calls the module:
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DetectModule = require(script.Detect)
local BulletModule = require(script.Bullet)
local MovementModule = require(script.Movement)
local TurrentsFolder = workspace.Turrents:GetChildren()
for _, turrent in ipairs(TurrentsFolder) do
if turrent then
while wait(1) do
local target = DetectModule.Detect()
if target then
BulletModule.shootBullet(ServerStorage.Bullet, false, false, turrent.Main.Gun, 50, target, workspace.Bullets)
MovementModule.moveTurrent(turrent.Main.Gun, target)
end
end
end
end
Module for moving the turrent:
llocal RunService = game:GetService("RunService")
local module = {}
function module.moveTurrent(turrent, target)
RunService.Heartbeat:Connect(function()
turrent.CFrame = CFrame.new(turrent.Position, target.Position)
end)
end
return module
Module for detecting player:
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local target = nil
local module = {}
function module.Detect()
if #Players:GetPlayers() <= 0 then
repeat wait()
print("Checking Players")
until #Players:GetPlayers() >= 1
end
for _, player in ipairs(Players:GetPlayers()) do
local character = player.Character
if character then
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoid and humanoidRootPart then
if humanoid.Health > 10 then
if not target then
target = humanoidRootPart
end
else
target = nil
end
return target
end
else
target = nil
end
end
end
return module
Module for shooting bullets:
local module = {}
function module.createBullet(bullet, anchored, canCollide, object, velocity, target, parent)
local newBullet = bullet:Clone()
newBullet.Anchored = anchored
newBullet.CanCollide = canCollide
newBullet.CFrame = object.CFrame
newBullet.Parent = parent
local newVelocity = Instance.new("BodyVelocity")
newVelocity.Parent = newBullet
newBullet.BodyVelocity.Velocity = object.CFrame.LookVector * velocity
end
function module.shootBullet(bullet, anchored, canCollide, object, target, velocity, parent)
module.createBullet(bullet, anchored, canCollide, object, target, velocity, parent)
end
return module
I’m all ears on how I can improve the readability of code and it’s efficiency.