Make View Models Server Side

I have made a view model for a flashlight item on the client side but I want everyone in the server to see the positions of each others arms in the view model.

I am not quite sure on how to go about this but I have seen it in other games like Phantom Forces and would like something similar.

(Also if anyone knows how to move the players head to where their camera is looking on server side that would be very helpful)

Local Script (Inside flashlight tool)

local tool = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local rs = game:GetService("RunService")
local leftArm = char:WaitForChild("Left Arm")
local rightArm = char:WaitForChild("Right Arm")
local uis = game:GetService("UserInputService")
local ViewModel
local equipped = false

local function setArmTransparency(transparency)
	rightArm.Transparency = transparency
	rightArm.LocalTransparencyModifier = 1 - transparency
	wait(0.1)
	rightArm.LocalTransparencyModifier = 0
end

tool.Equipped:Connect(function()
	equipped = true
	if not Camera:FindFirstChild("ViewModel") then
		ViewModel = game.ReplicatedStorage.ViewModel:Clone()
		ViewModel.Parent = Camera
	end
	setArmTransparency(1)
end)

tool.Unequipped:Connect(function()
	equipped = false
	if ViewModel then
		ViewModel:Destroy()
		ViewModel = nil
	end
	setArmTransparency(0)
end)

local swayCF = CFrame.new()

rs.RenderStepped:Connect(function()
	if player.Character and player.Character:FindFirstChild("Humanoid") and player.Character.Humanoid.Health <= 0 then
		if ViewModel then
			ViewModel:Destroy()
			ViewModel = nil
		end
	end

	if equipped and ViewModel then
		ViewModel["Right Arm"].Color = char["Body Colors"].RightArmColor3
		if char.Shirt then
			ViewModel.Shirt.ShirtTemplate = char.Shirt.ShirtTemplate
		end

		ViewModel:SetPrimaryPartCFrame(Camera.CFrame)
		for _, v in pairs(ViewModel:GetChildren()) do
			if v:IsA("BasePart") then
				v.CanCollide = false
			end
		end
		local mouseDelta = uis:GetMouseDelta()/50

		local swayX = math.clamp(mouseDelta.X, -0.2,0.2)
		local swayY = math.clamp(mouseDelta.Y, -0.2,0.2)

		swayCF = swayCF:Lerp(CFrame.new(swayX,swayY, 0), 0.3)
		Camera.ViewModel:SetPrimaryPartCFrame(Camera.CFrame * swayCF)
	end
end)

One way of doing this is to use a Remote Function to constantly update the player’s arm positions, although this might be quite heavy on performance.

Yeah, I wanted to see if there was something I was missing other than using Remote Functions but what if I just checked where their camera was looking and based the arm position off of that still using remote functions but every 0.1 seconds and it would send less data.

1 Like

Ah that’s definitely a better solution since it would send less data like you said. I’d also recommend using Coroutines to run the remotefunction for each player “simultaneously”

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.