Im having trouble replicating rotation and position for flashlight

as i said in the title im having trouble rotating the flashlight to the heads rotation on the server

why im doing this is obviously because the fact that you cant get the camera in a server script but right now it doesnt rotate

as for the positions what i mean is if i run on local it doesnt lag behind but on server it does what do i do to fix that

server script

local viewmodel = game.ReplicatedStorage.VmStorage.Flashlight:Clone()

game.ReplicatedStorage.EventStorage.SecondaryFlashlightEvents.on.OnServerEvent:Connect(function(plr:Player,swayCF,SwayX,SwayY,cam,CameraCF,headCF,CamCFRot,camPar)

headCF = plr.Character:WaitForChild(“Head”).CFrame

–CameraCF = cam

viewmodel.Parent = workspace.VmStorage

plr.Character:WaitForChild(“Right Arm”).Transparency = 1

viewmodel.Handle[“Right Arm”].Color = plr.Character:WaitForChild(“Right Arm”).Color

CamCFRot = CameraCF.Rotation

local SwayX = 3

local SwayY = 5

local swayCF = CFrame.new()

local head = plr.Character:WaitForChild(“Head”)

local HeadCF = head.CFrame

local v3 = Vector3.new(headCF.Rotation.X,headCF.Rotation.Y,headCF.Rotation.Z)

local rotation = plr.Character:WaitForChild(“Head”).CFrame

local weld = Instance.new(“WeldConstraint”)

weld.Part0 = viewmodel.PrimaryPart

weld.Part1 = plr.Character:WaitForChild(“Right Arm”)

local x,y,z = rotation:ToOrientation()

local swayCF = swayCF:Lerp(CFrame.new(math.sin(-y)*SwayX,math.sin(x)*SwayY,0), .2)

while task.wait() do

viewmodel:SetPrimaryPartCFrame(headCF * CFrame.new(v3) * CFrame.new(-0.6,-0.4,0.7) * swayCF)

end

end)

local script

local player = game.Players.LocalPlayer
local char = player.Character
local cam = workspace.CurrentCamera
local viewmodel = game.ReplicatedStorage.VmStorage:WaitForChild(“Flashlight”):Clone() or game.ReplicatedStorage.VmStorage.Flashlight:Clone() – just incase it’s already loaded
viewmodel.Parent = game.ReplicatedStorage.VmStorage[“ClonedVm’s”]
local run = game:GetService(“RunService”)
local CameraCF = CFrame.new()
local swayCF = CFrame.new()
local SwayX = 3
local SwayY = 5
local equip = game.ReplicatedStorage.BoolStorage.Flashlight
local uis = game:GetService(“UserInputService”)
task.wait()
local audio = script[“Flashlight Click”]
uis.InputBegan:Connect(function(inp)
if inp.KeyCode == Enum.KeyCode.F then
print(“alive”)
flashlight()

end	

end)

function flashlight()
local val = game.ReplicatedStorage.BoolStorage.StartupGui.InGui
if val.Value == false then
if equip.Value == false then
changeIfSwitch()
game.Players.LocalPlayer.Character:WaitForChild(“Right Arm”).Transparency = 1
audio:Play()
equip.Value = true
run.RenderStepped:Connect(function()
if game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).Health > 0 then
local rotation = workspace.CurrentCamera.CFrame:toObjectSpace(CameraCF)
local x,y,z = rotation:ToOrientation()
swayCF = swayCF:Lerp(CFrame.new(math.sin(-y)*SwayX,math.sin(x)*SwayY,0), .2)
CameraCF = cam.CFrame
local headCF = player.Character:WaitForChild(“Head”).CFrame
local CamCFRot = CameraCF.Rotation
local v3 = Vector3.new(CameraCF.Rotation.X,CameraCF.Rotation.Y,CameraCF.Rotation.Z)
local camPar = game.Workspace.CurrentCamera
game.ReplicatedStorage.EventStorage.SecondaryFlashlightEvents.on:FireServer(swayCF,SwayX,SwayY,cam,CameraCF,headCF,viewmodel,CamCFRot,camPar)
end
end)
else
equip.Value = false
viewmodel.Parent = game.ReplicatedStorage.VmStorage[“ClonedVm’s”]
game.Players.LocalPlayer.Character:WaitForChild(“Right Arm”).Transparency = 0
audio:Play()
end
end
end

function hideFlashlightIfDead()
if game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).Health <= 0 then
equip.Value = false
viewmodel.Parent = game.ReplicatedStorage.VmStorage[“ClonedVm’s”]
game.Players.LocalPlayer.Character:WaitForChild(“Right Arm”).Transparency = 0
end
end

function changeIfSwitch()
if game.ReplicatedStorage.BoolStorage.ThirdPerson.Switch.Value == true then
SwayX = 1
else
SwayX = 2.5
end
end

Your code correctly formatted:
serverscript

local viewmodel = game.ReplicatedStorage.VmStorage.Flashlight:Clone()

game.ReplicatedStorage.EventStorage.SecondaryFlashlightEvents.on.OnServerEvent:Connect(function(plr:Player,swayCF,SwayX,SwayY,cam,CameraCF,headCF,CamCFRot,camPar)

headCF = plr.Character:WaitForChild(“Head”).CFrame

–CameraCF = cam

viewmodel.Parent = workspace.VmStorage

plr.Character:WaitForChild(“Right Arm”).Transparency = 1

viewmodel.Handle[“Right Arm”].Color = plr.Character:WaitForChild(“Right Arm”).Color

CamCFRot = CameraCF.Rotation

local SwayX = 3

local SwayY = 5

local swayCF = CFrame.new()

local head = plr.Character:WaitForChild(“Head”)

local HeadCF = head.CFrame

local v3 = Vector3.new(headCF.Rotation.X,headCF.Rotation.Y,headCF.Rotation.Z)

local rotation = plr.Character:WaitForChild(“Head”).CFrame

local weld = Instance.new(“WeldConstraint”)

weld.Part0 = viewmodel.PrimaryPart

weld.Part1 = plr.Character:WaitForChild(“Right Arm”)

local x,y,z = rotation:ToOrientation()

local swayCF = swayCF:Lerp(CFrame.new(math.sin(-y)*SwayX,math.sin(x)*SwayY,0), .2)

while task.wait() do

viewmodel:SetPrimaryPartCFrame(headCF * CFrame.new(v3) * CFrame.new(-0.6,-0.4,0.7) * swayCF)

end

end)

local script

local player = game.Players.LocalPlayer
local char = player.Character
local cam = workspace.CurrentCamera
local viewmodel = game.ReplicatedStorage.VmStorage:WaitForChild(“Flashlight”):Clone() or game.ReplicatedStorage.VmStorage.Flashlight:Clone() – just incase it’s already loaded
viewmodel.Parent = game.ReplicatedStorage.VmStorage[“ClonedVm’s”]
local run = game:GetService(“RunService”)
local CameraCF = CFrame.new()
local swayCF = CFrame.new()
local SwayX = 3
local SwayY = 5
local equip = game.ReplicatedStorage.BoolStorage.Flashlight
local uis = game:GetService(“UserInputService”)
task.wait()
local audio = script[“Flashlight Click”]
uis.InputBegan:Connect(function(inp)
if inp.KeyCode == Enum.KeyCode.F then
print(“alive”)
flashlight()

end	
end)

function flashlight()
local val = game.ReplicatedStorage.BoolStorage.StartupGui.InGui
if val.Value == false then
if equip.Value == false then
changeIfSwitch()
game.Players.LocalPlayer.Character:WaitForChild(“Right Arm”).Transparency = 1
audio:Play()
equip.Value = true
run.RenderStepped:Connect(function()
if game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).Health > 0 then
local rotation = workspace.CurrentCamera.CFrame:toObjectSpace(CameraCF)
local x,y,z = rotation:ToOrientation()
swayCF = swayCF:Lerp(CFrame.new(math.sin(-y)*SwayX,math.sin(x)*SwayY,0), .2)
CameraCF = cam.CFrame
local headCF = player.Character:WaitForChild(“Head”).CFrame
local CamCFRot = CameraCF.Rotation
local v3 = Vector3.new(CameraCF.Rotation.X,CameraCF.Rotation.Y,CameraCF.Rotation.Z)
local camPar = game.Workspace.CurrentCamera
game.ReplicatedStorage.EventStorage.SecondaryFlashlightEvents.on:FireServer(swayCF,SwayX,SwayY,cam,CameraCF,headCF,viewmodel,CamCFRot,camPar)
end
end)
else
equip.Value = false
viewmodel.Parent = game.ReplicatedStorage.VmStorage[“ClonedVm’s”]
game.Players.LocalPlayer.Character:WaitForChild(“Right Arm”).Transparency = 0
audio:Play()
end
end
end

function hideFlashlightIfDead()
if game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).Health <= 0 then
equip.Value = false
viewmodel.Parent = game.ReplicatedStorage.VmStorage[“ClonedVm’s”]
game.Players.LocalPlayer.Character:WaitForChild(“Right Arm”).Transparency = 0
end
end

function changeIfSwitch()
if game.ReplicatedStorage.BoolStorage.ThirdPerson.Switch.Value == true then
SwayX = 1
else
SwayX = 2.5
end
end```

Please don’t repost your exact same question in different posts:I'm having trouble replicating the flashlight to the server. If you post it twice others can’t see what you’ve already tried and what other people have suggested.

And to format your code so it’s easy to read here, copy it from Studio and Paste it here, then put 3 backticks (```) before and after it so we can read it properly.

sorry i thought this was different enough because in that i was having trouble even getting it to show but i figured that out so i posted this cause i was facing a different problem

is there anyway to get rid of that latency on the position so when moving it doesnt lag behind