Hi, I’m making a Turret. I’ve tried it on Studio and it works fine, which I assume is because it’s not as laggy. But on the game, it seems that it lags far more than the Studio. I assume if I change my BodyGyro to Client-Sided things should smooth out. But if there are any other better options, I’d like to know!
-
-
- EDIT: I’m no longer working on this turret as of yet, but I still can’t find any solutions, but I will still check replies and fix the turret. * * *
-
ServerScript:
Parented to the Turret’s Seat
--//VARIABLES//--
local TurretSeat = script.Parent
local TurretCamera = game.Workspace.Turret.TurretCamera
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TurretFolder = ReplicatedStorage:FindFirstChild("Turret")
local FireTurret = TurretFolder.FireTurret
local MoveTurret = TurretFolder.MoveTurret
local OnSeated = TurretFolder.OnSeated
local RunService = game:GetService("RunService")
local TurretHead = TurretSeat.Parent.TurretHead
local Laser = game.ServerStorage.Laser:Clone()
local Player
--//SETTINGS//--
local IsSeated = false
local ShootDistance = 5000
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {workspace.Turret}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
local LaserParams = RaycastParams.new()
LaserParams.FilterDescendantsInstances = {Laser}
LaserParams.FilterType = Enum.RaycastFilterType.Blacklist
--//FUNCTIONS//--
--//PRE-START//--
local BodyGyro = Instance.new("BodyGyro")
local BodyPosition = Instance.new("BodyPosition")
BodyPosition.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
BodyPosition.Parent = game.Workspace.Turret.Stand
BodyPosition.P = 1000
BodyPosition.D = 100
BodyPosition.Position = game.Workspace.Turret.Stand.Position
BodyGyro.Parent = game.Workspace.Turret.Stand
BodyGyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
BodyGyro.P = 250
BodyGyro.D = 50
--------------------------------------------------------------------
TurretSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
if TurretSeat.Occupant then
-- Someone is sitting
IsSeated = true
local Character = TurretSeat.Occupant.Parent
Player = game.Players:GetPlayerFromCharacter(Character)
OnSeated:FireClient(Player, IsSeated)
else
IsSeated = false
OnSeated:FireClient(Player, IsSeated)
end
end)
MoveTurret.OnServerEvent:Connect(function(player, mousepos)
BodyGyro.CFrame = mousepos
game.Workspace.Turret.PrimaryPart:SetNetworkOwner(player)
end)
FireTurret.OnServerEvent:Connect(function(player)
local Raycast = workspace:Raycast(TurretHead.Position, TurretHead.CFrame.LookVector * ShootDistance, RayParams)
if Raycast then
print(tostring(Raycast.Instance))
local Explosion = Instance.new("Explosion")
Explosion.Parent = workspace
Explosion.Position = Raycast.Position
wait(0.5)
Explosion:Destroy()
end
end)
RunService.Heartbeat:Connect(function()
local Raycast = workspace:Raycast(TurretHead.Position, TurretHead.CFrame.LookVector * ShootDistance, LaserParams)
if Raycast then
Laser.Parent = workspace
Laser.Position = Raycast.Position
end
end)
ClientScript:
Parented to StarterCharacterScripts
--//VARIABLES//--
local Character = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TurretFolder = ReplicatedStorage:FindFirstChild("Turret")
local OnSeated = TurretFolder.OnSeated
local MoveTurret = TurretFolder.MoveTurret
local FireTurret = TurretFolder.FireTurret
local LocalPlayer = game.Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local CurrentCamera = game.Workspace.CurrentCamera
local TurretCamera = game.Workspace.Turret.TurretCamera
local RunService = game:GetService("RunService")
local IsSeatedValue
--//SETTINGS//--
local MouseIcon = "http://www.roblox.com/asset/?id=7059266885"
--//FUNCTIONS//--
--//PRE-START//--
local BodyGyro = Instance.new("BodyGyro")
BodyGyro.Parent = game.Workspace.Turret.Stand
BodyGyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
BodyGyro.P = 250
BodyGyro.D = 50
--------------------------------------------------------
OnSeated.OnClientEvent:Connect(function(IsSeated)
IsSeatedValue = IsSeated
if IsSeated then
Mouse.Icon = MouseIcon
repeat CurrentCamera.CameraType = Enum.CameraType.Scriptable
until CurrentCamera.CameraType == Enum.CameraType.Scriptable
else
repeat CurrentCamera.CameraType = Enum.CameraType.Custom
until CurrentCamera.CameraType == Enum.CameraType.Custom
Mouse.Icon = ""
end
end)
RunService.RenderStepped:Connect(function()
if IsSeatedValue then
MoveTurret:FireServer(Mouse.Hit)
end
end)
RunService.RenderStepped:Connect(function()
if IsSeatedValue then
CurrentCamera.CFrame = TurretCamera.CFrame
end
end)
Mouse.Button1Down:Connect(function()
FireTurret:FireServer()
end)