So I made a tool which places a turret when you activate it, it’s pretty simple.
The problem is that it only sometimes/rarely fires.
My theory is that it’s because of the “creator” value.
If it found a player in workspace, and that character name is the value of “creator”. Then it will not fire
Script:
local Top = script.Parent:WaitForChild("Top")
local Bullet = game:GetService("ReplicatedStorage"):WaitForChild("Bullet")
local fireRate = 0.1
local bulletDamage = 3
local bulletSpeed = 500
local aggroDist = 100
while wait(fireRate) do
-- Target is Realistic to shoot (?)
local target = nil
for i,v in pairs(workspace:GetChildren()) do
if script.Parent:WaitForChild("creator").Value == v.Name then else
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and human.Health > 0 then
if (torso.Position - Top.Position).magnitude < aggroDist then
local bulletRay = Ray.new(Top.Position, (torso.Position - Top.Position).Unit * aggroDist)
local hit, position = workspace:FindPartOnRayWithIgnoreList(bulletRay, {Top})
if hit == torso then
target = torso
end
end
end
end
end
if target then
local torso = target
Top.CFrame = CFrame.new(Top.Position, torso.Position)
local newBullet = Bullet:Clone()
newBullet.Position = Top.Position
newBullet.Anchored = false
newBullet.Parent = workspace
newBullet.CFrame = CFrame.new(newBullet.Position, target.Position)
newBullet.Velocity = Top.CFrame.lookVector * bulletSpeed
local cloneofthesound = Top.Sound:Clone()
cloneofthesound.Parent = Top
cloneofthesound:Play()
spawn(function() wait(2) cloneofthesound:Destroy() end)
newBullet.Touched:Connect(function(objectHit)
local human = objectHit.Parent:FindFirstChild("Humanoid")
if human then
human:TakeDamage(bulletDamage)
end
end)
end
end
Image:
The scripts inside of the turret are disabled since it has not been cloned yet.
The other “Script” will simply destroy the turret after a certain amount of time, ignore that.