First, make a script inside the turret like so.
turretthingy = script.Parent
distance = 30
function getHumans()
for _, v in pairs(game.Players:GetChildren()) do
if v.Character ~= nil and v.Character:FindFirstChild("Torso") ~= nil and v.Character:FindFirstChild("Humanoid") ~= nil and v.Character.Humanoid.Health > 0 then
if (v.Character.Torso.Position - turretthingy.[TurretBrickName].Position).magnitude <= 80 then
return v.Character.Torso
end
end
end
return nil
end
function aim(targ)
local dir = targ.Position - script.Parent.Barrel.Position
dir = computeDirection(dir)
local mag = (targ.Position - script.Parent.[TurretMainBrickName].Position).magnitude
local spawnPos = script.Parent.Barrel.Position
local pos = spawnPos + (dir * (math.ceil(mag)/2))
bin.Barrel.BodyGyro.cframe = CFrame.new(pos, pos + dir)
local look = turretthingy.[TurretBrickName].CFrame.lookVector
local p = Instance.new("Part")
p.formFactor = "Symmetric"
p.Size = Vector3.new(1,1,1)
p.TopSurface = "Smooth"
p.Shape = "Ball"
p.BottomSurface = "Smooth"
p.Name = "Bullet"
p.BrickColor = BrickColor.new("Really Black")
p.CFrame = script.Parent.Shooter.CFrame + (1.5*look)
local b = Instance.new("BodyVelocity")
b.velocity = (50*look)
b.Parent = p
p.Parent = workspace
local sc = script.Damage:clone()
sc.Disabled = false
sc.Parent = p
end
function computeDirection(vec)
local lenSquared = vec.magnitude * vec.magnitude
local invSqrt = 1 / math.sqrt(lenSquared)
return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end
while true do
wait(0.2)
local target = getHumans()
if target ~= nil then
aim(target)
end
end
Then, add a damage script inside, i.e.
game:GetService("Debris"):AddItem(script.Parent, 4)
deb = false
function TACO(h)
if h.Parent ~= nil and h.Parent:FindFirstChild("Humanoid") ~= nil then
if deb == false then
deb = true
h.Parent.Humanoid:TakeDamage(Whatever damage you want.)
wait()
script.Parent:remove()
end
end
end
script.Parent.Touched:connect(TACO) |