Advice needed on how to make gun aiming more optimized on the server



I don’t mind showing the code to others as it is open source. I need some advice on how to make it more optimized whilst keeping it serversided. Anything is appreciated.

I can’t do much since the code isn’t posted here in a code block, but I suggest breaking up your RemoteEvents into two called ‘Unequip’ and ‘Turn.’ As you add more to your guns code, it’ll get messier with just one RemoteEvent.

Also you’d be sending over less data (no request var).

2 Likes

Hey there, thanks for replying. I took the code and decided to put it into two code blocks, I hope this will help you with helping me.

-- Server for Client, this is so it works in FE

local update = script:WaitForChild('Update')
local Neck_Original = CFrame.new(0, 1, 0, -1, -0, -0, 0, 0, 1, 0, 1, 0)

update.OnServerEvent:connect(function(player, request, mouseposition)
	local Prone = player.Backpack:FindFirstChild('PlayerScripts').Proned
	local Torso = player.Character:FindFirstChild('Torso')
	local Arm = Torso:FindFirstChild('Right Shoulder')
	local Arm2 = Torso:FindFirstChild('Left Shoulder')
	if (request == 'Turn') then
		local Offset_O = (Torso.Position.y - mouseposition.y)/100 --100 (mouse offset to direction)
		local Mag = (Torso.Position-mouseposition).magnitude/120 --80 (cone of y looking)
		local Offset = Offset_O/Mag
		local NeckWeld = Torso.Neck
		if Prone.Value then
			NeckWeld.C0 = Neck_Original * CFrame.fromEulerAnglesXYZ(Offset-math.rad(90), 0, 0)
		else
			NeckWeld.C0 = Neck_Original * CFrame.fromEulerAnglesXYZ(Offset, 0, 0)
		end
		if Arm ~= nil then
			if Prone.Value then
				Arm.C0 = CFrame.new(1, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0) * CFrame.fromEulerAnglesXYZ(0, 0, -Offset+math.rad(90))
			else
				Arm.C0 = CFrame.new(1, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0) * CFrame.fromEulerAnglesXYZ(0, 0, -Offset)
			end
		end
		if Arm2 ~= nil then
			if Prone.Value then
				Arm2.C0 = CFrame.new(-1, 0.5, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0) * CFrame.fromEulerAnglesXYZ(0, 0, Offset-math.rad(90))
			else
				Arm2.C0 = CFrame.new(-1, 0.5, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0) * CFrame.fromEulerAnglesXYZ(0, 0, Offset)
			end
		end
	elseif (request == 'Unequip') then
		Arm.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(0, math.pi/2, 0) -- reset right arm
		Arm2.C0 = CFrame.new(-1, 0.5, 0) * CFrame.Angles(0, -math.pi/2, 0) -- reset left arm
		Torso.Neck.C0 = Neck_Original
	end
end)
wait()
-- Modified By GraphicsSettings, Original is unknown
-- Removed Unnessecary code and improved the syntax
-- Better than toObjectSpace?

local Tool = script.Parent.Parent
local update = script.Parent:WaitForChild('Update')
local Neck_Original = CFrame.new(0, 1, 0, -1, -0, -0, 0, 0, 1, 0, 1, 0)

local RS = game:GetService("RunService").RenderStepped

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Torso = Character:WaitForChild('Torso')

Tool.Unequipped:connect(function()
	update:FireServer('Unequip')
end)

Tool.Equipped:connect(function(curr_mouse)
	wait(.2)
	curr_mouse.TargetFilter = workspace
	while RS:wait() do
		if Tool.Parent.className ~= 'Model' then
			break
		end
		local holster = Tool.Holstered
		if not holster.Value then
			update:FireServer('Turn', curr_mouse.Hit.p)
		elseif holster.Value then
			update:FireServer('Unequip')	
		end
	end
end)
1 Like

I’m trying to make a third person aiming script for my guns but I don’t know how to then I saw this and I wanted to know how you made your aiming script and where you placed it

1 Like