You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve?
Fix Turret lag. -
What is the issue?
Turret system works fine, but once placed in a game with many assets it lags behind. It’s to the point where the bullets render useless and don’t even move from the barrel. I am still very new to scripting and almost all of my scripts heavily depend on information I find throughout the devforum.
This is all handled in a server script, which I presume to be the problem based on what I’ve read. However, due to my lack of knowledge, I’m unsure how to handle the Bullets through the client.
local p = script.Parent
local CameraHead = p.Camera:WaitForChild("CameraHead")
local CameraWeld = p.CameraMount:WaitForChild("CameraHead")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
4,
Enum.EasingStyle.Sine,
Enum.EasingDirection.InOut,
-1,
true,
1
)
local goal = {C0 = CameraWeld.C0 * CFrame.Angles(0, math.rad(-90), 0)}
local tween = tweenService:Create(CameraWeld, tweenInfo, goal)
tween:Play()
function visualizeRay(origin, direction)
local part = Instance.new("Part")
part.Transparency = 0.98
part.Anchored = true
part.CanCollide = false
part.Material = Enum.Material.Neon
part.Color = Color3.fromRGB(255, 0, 0)
part.Size = Vector3.new(0.2, 0.2, direction.Magnitude)
part.CFrame = CFrame.new(origin, origin + direction) * CFrame.new(0, 0, -direction.Magnitude / 2)
part.Parent = workspace
game:GetService("Debris"):AddItem(part, 0.1)
end
function raycast()
local results = {}
for _, attachment in next, CameraHead:GetChildren() do
if attachment:IsA("Attachment") and attachment.Name == "RaycastAttachment" then
for _, directPart in next, CameraHead:GetChildren() do
if directPart:IsA("BasePart") and directPart.Name == "direct" then
local origin = attachment.WorldPosition
local length = 50
local direction = directPart.CFrame.LookVector * length
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {attachment, CameraHead, p}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
visualizeRay(origin, direction)
results[attachment] = workspace:Raycast(origin, direction, raycastParams)
if results[attachment] then
local hitInstance = results[attachment].Instance
local char = hitInstance.Parent
if char:FindFirstChild("HumanoidRootPart") then
CameraHead.Alert:Play()
tween:Pause()
local function RandomCone(axis: Vector3, angle: number)
local cosAngle = math.cos(angle)
local z = 1 - math.random()*(1 - cosAngle)
local phi = math.random()*math.pi*2
local r = math.sqrt(1 - z*z)
local x = r * math.cos(phi)
local y = r * math.sin(phi)
local vec = Vector3.new(x, y, z)
if axis.Z > 0.9999 then
return vec
elseif axis.Z < -0.9999 then
return -vec
end
local orth = Vector3.zAxis:Cross(axis)
local rot = math.acos(axis:Dot(Vector3.zAxis))
return CFrame.fromAxisAngle(orth, rot) * vec
end
local function LaunchProjectile()
local SPREAD = math.rad(45)
local bulletSpeed = 89
local bulletPrefab = game.ReplicatedStorage:WaitForChild("Bullet")
local barrel = p.Camera.Blaster.Barrel
local bullet = bulletPrefab:Clone()
local targetHit = false
bullet.CFrame = barrel.CFrame
bullet.Parent = workspace
local spreadAmount = math.rad(20)
local bodyVeloc = Instance.new("BodyVelocity",bullet)
bullet.BodyVelocity.Velocity = RandomCone(CameraHead.CFrame.LookVector, spreadAmount) * bulletSpeed
game.Debris:AddItem(bullet, 2)
bullet.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil and targetHit == false then
humanoid:TakeDamage(35)
targetHit = true
bullet.flesh_impact_bullet1:Play()
end
end)
end
for i = 1, 36 do
LaunchProjectile()
CameraHead["Machine Gun single shoot"]:Play()
wait(0.1)
end
tween:Play()
end
end
end
end
end
end
end
while true do
raycast()
wait(0.1)
end