What do you want to achieve? Keep it simple and clear!
–
What is the issue? Include screenshots / videos if possible!
No errors. Output.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Code of a Turret.
local Bullet = game.ReplicatedStorage:WaitForChild("Bullet")
local Players = game:GetService("Players")
local Turret = script.Parent
local FireRate = 0.001
local BulletDamage = 3.5
local BulletSpeed = 150
local AggroDistance = 100
while wait(FireRate) do
local Target = nil
for _, Player in pairs(Players:GetChildren()) do
end
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:WaitForChild("Humanoid")
local Torso = Character:FindFirstChild("Torso")
if Humanoid and Torso and Humanoid.Health > 0 then
if (Torso.Position - Turret.Position).Magnitude < AggroDistance then
Target = Torso
end
end
end)
end)
if Target then
local Torso = Target
Turret.CFrame = CFrame.new(Turret.Position, Torso.Position)
local newBullet = Bullet:Clone()
newBullet.Position = Turret.Position
newBullet.Parent = game.Workspace
newBullet.LinearVelocity.VectorVelocity = Turret.CFrame.LookVector * BulletSpeed
newBullet.Touched:Connect(function(objectHit)
local Humanoid = objectHit.Parent:FindFirstChild("Humanoid")
if Humanoid then
Humanoid:TakeDamage(BulletDamage)
end
end)
end
end
well you could do
And put this inside of a local script in starter player script
local function findT(target)
local maxd = 35
local player= nil
for i,v in ipairs(game.Players:GetPlayers()) do
if v ~= nil and v.Character then
local currentD = (v.Character.PrimaryPart.Position - target:WaitForChild("Tip").Position).Magnitude
if currentD < maxd then
player= v.Character
maxd = currentD
end
end
end
return player
end
local function TurnT(turret,player)
local head = turret:WaitForChild("Head")
local pp = player.PrimaryPart
head.CFrame = CFrame.new(head.CFrame.p,player.Head.CFrame.p) * CFrame.Angles(math.rad(0),0,math.rad(90))
end
local function takedmg(turret)
rs.RenderStepped:Connect(function()
local player= findT(turret)
if monster then
TurnT(turret,player)
local configs = turret:WaitForChild("Configurations")
local dmg = configs.Dmg
local reloadt = configs.Reload
if debounce == false then
debounce = true
game.ReplicatedStorage.Events.Re:FireServer(player,dmg.Value)
task.wait(reloadt.Value)
debounce = false
end
end
task.wait(0.1)
end)
end
takedmg()