Hello everyone! I am trying to make a tower defense game named “turret shot” and i cant seem to get the turret working
The Code
local rep = game:GetService("ReplicatedStorage")
local bullet = rep:WaitForChild("bullet")
local turret = script.Parent
local firerate = 2
local bulletdamage = 5
local bulletspeed = 400
local aggrodist = 200
while wait(firerate) do
local target = nil
for i, v in pairs(game.Workspace.Folder:GetChildren()) do
local hum = v:FindFirstChild("Humanoid")
local torso = v:WaitForChild("Torso")
if hum and torso and hum.Health > 0 then
if (torso.Position - turret.Position).magnitude < aggrodist then
ray = Ray.new(turret.Position, (torso.Position - turret.Position).Unit * aggrodist)
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, {turret})
if hit == torso then
target = torso
end
end
end
end
if target then
local torso = target
turret.CFrame = CFrame.new(turret.Position, torso.Position)
local new = bullet:Clone()
new.Position = turret.Position
new.Parent = game.Workspace
new.Velocity = turret.CFrame.LookVector * bulletspeed
new.Touched:Connect(function(objectHit)
local human = objectHit.Parent:FindFirstChild("Humanoid")
if human then
human:TakeDamage(bulletdamage)
else
wait(5)
new:Destroy()
end
end)
end
end
The script has no errors but it doesnt seem to work.
If any of you have feedback please send it!
thanks, Icy