How to make Aiming Animations appear on the Server?

Hello!

So i am wondering how do you make aiming animations with Player Arms visible on the Server?

Expected Behavior: (Sorry if mp4 is stretched)
Animations Play and Camera FOV Changes along with DepthOfField (This is the Client Version)


Actual Behavior:
Animations don’t work on the Server.

I have tried RemoteEvents, Functions, and i have no idea how to do it without it not being visible on the Server or Causing errors.

My Current Code on a LocalScript:

local GS = require(script.Parent.WeaponSetup) -- ModuleScript, stores all Animation Values + bools
MainClient = script.Parent.Client
A = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(GS.Client.Aim)
GS.IsActive = true

while GS.IsActive == true and GS.IsAiming == false do
	wait()
GS.UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Q then
		GS.IsAiming = true
		
		GS.TweenService:Create(GS.Camera, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FieldOfView = 40}):Play()
		GS.TweenService:Create(GS.Lighting.DepthOfField, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FarIntensity = 1}):Play()
		MainClient.Fire.AnimationId = GS.AimFireAnimation -- Replaces the Normal Shooting Animation with the Aiming Shooting Version
			A:Play()


	end
	
GS.UserInputService.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.Q then
	GS.IsAiming = false

		MainClient.Fire.AnimationId = GS.FireAnimation -- Replaces the Aiming Shooting Version with the Normal Shooting Version
		GS.TweenService:Create(GS.Camera, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FieldOfView = 120}):Play()
		GS.TweenService:Create(GS.Lighting.DepthOfField, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FarIntensity = 0}):Play()
				
		A:Stop()

	end
end)	
	
	end)
	if GS.IsActive == false then
		GS.Camera.FieldOfView = 120
		A:Stop()
		GS.IsAiming = false

		break
	end

script.Parent.Unequipped:Connect(function() -- Closes Aiming for when unequipping
		
		GS.Camera.FieldOfView = 120
		GS.Lighting.DepthOfField.FarIntensity = 0
		GS.IsActive = false
		GS.IsAiming = false
		A:Stop()
		MainClient.Fire.AnimationId = GS.FireAnimation
		script.Enabled = false
end)
end	

What I want is for the Players Animation to play on the Server, not the Client, Can you help?
All Replies are helpful!
Thanks.

One way this can be done is the following pseudo code.

You can send a signal to the server as a “hey Im scoping in”
Then the server fires a signal to every client, “hey this person is scoping in”
Then the client is connected to that signal and says, "if the server is telling me that person is scoping on me, then I will play scoping animation on their avatar.

Yes, but i am wondering how exactly do i do that?

Here is a base to go with, change "toggleScopeRemoteEvent " to the remote event you will use., and on the comment provided on the client side, play the animation depending on what the boolean is.

--Client
local Players = game:GetService('Players')
local toggleScopeRemoteEvent = ...

local aiming = false

local function playerAdded(player: Player)
	local function updateScopingForPlayer(plr, isAiming)
		--Play animations here, "isAiming" is a boolean whether or not a player is aiming.
	end
	
	local isAiming = player:GetAttribute("Aiming")==true
	updateScopingForPlayer(player, isAiming)
	
	player:GetAttributeChangedSignal("Aiming"):Connect(function()
		local isAiming = player:GetAttribute("Aiming")==true
		updateScopingForPlayer(player, isAiming)
	end)
end

GS.UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Q then
		aiming = not aiming
		toggleScopeRemoteEvent:FireServer(aiming)
	end
end)

for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
	playerAdded(player)
end

Players.PlayerAdded:Connect(playerAdded)

-- Server
local toggleScopeRemoteEvent = ...

toggleScopeRemoteEvent.OnServerEvent:Connect(function(player, isAiming)
	player:SetAttribute("Aiming", isAiming)
end)

Workspace.xGOA7x.Tool1.Aiming:26: attempt to index nil with 'FireServer'

set the variable toggleScopeRemoteEvent to a remote event on ReplicatedStorage on both scripts.

Appears to work, the only issue is that it wont change the bool of aiming

image
This line right here changes the state of aiming. (everytime Q is pressed, its inverting it.)

@KinqAndi
Yes, i understand that, the issue is that it wont do it.

local GS = require(script.Parent.WeaponSetup)
local Players = game:GetService('Players')
local toggleScopeRemoteEvent = script.Parent.Aim

local aiming = false

local function playerAdded(player: Player)
	local function updateScopingForPlayer(plr, isAiming)
		
		GS.Player.Character:FindFirstChild("Humanoid"):LoadAnimation(GS.Client.Aim):Play()


	end

	local isAiming = player:GetAttribute("Aiming")==true
	updateScopingForPlayer(player, isAiming)

	player:GetAttributeChangedSignal("Aiming"):Connect(function()
		local isAiming = player:GetAttribute("Aiming")==true
		updateScopingForPlayer(player, isAiming)
	end)
end

GS.UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Q then
		aiming = not aiming
		
		toggleScopeRemoteEvent:FireServer(aiming)
	end
end)

for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
	playerAdded(player)
end

Players.PlayerAdded:Connect(playerAdded)

is toggleScopeRemoteEvent a remote event and is it on replicated storage?

its a RemoteEvent, i checked and yes, it is, its inside the tool however. even putting it inside ReplicatedStorage didnt help

You have to put the remote event on Replicated Storage, and connect to it from both scripts.

I changed it on both, its still is doing the exact same thing.

@KinqAndi I appeared to have fixed it by doing this:

local GS = require(script.Parent.WeaponSetup)
local Players = game:GetService('Players')
local toggleScopeRemoteEvent = game.ReplicatedStorage.Aim
local A = GS.Player.Character:FindFirstChild("Humanoid"):LoadAnimation(GS.Client.Aim)

local aiming = false

local function playerAdded(player: Player)
	local function updateScopingForPlayer(plr, isAiming)
		if aiming == true then
		A:Play()
			--GS.Client.Fire.AnimationId = GS.AimFireAnimation
			--GS.TweenService:Create(GS.Camera, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FieldOfView = 20}):Play()
			--GS.TweenService:Create(GS.Lighting.DepthOfField, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FarIntensity = 1}):Play()
		elseif aiming == false then
			A:Stop()
			--GS.Client.Fire.AnimationId = GS.FireAnimation
			--GS.TweenService:Create(GS.Camera, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FieldOfView = 120}):Play()
			--GS.TweenService:Create(GS.Lighting.DepthOfField, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FarIntensity = 0}):Play()
		end
	end

	local isAiming = player:GetAttribute("Aiming")==true
	updateScopingForPlayer(player, isAiming)

	player:GetAttributeChangedSignal("Aiming"):Connect(function()
		local isAiming = player:GetAttribute("Aiming")==true
		updateScopingForPlayer(player, isAiming)
	end)
end

GS.UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Q then
		aiming = not aiming
		
		toggleScopeRemoteEvent:FireServer(aiming)
	end
end)
GS.UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Q then
		aiming = not aiming

		toggleScopeRemoteEvent:FireServer(aiming)
	end
end)

GS.Tool.Unequipped:Connect(function()
	aiming = false
	A:Stop()
end)

for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
	playerAdded(player)
end

Players.PlayerAdded:Connect(playerAdded)

i need to test it out before marking it as the solution

Update:
the aiming = not aiming was causing issues for me, it fliped itself so when i tried aiming, it would stop aiming when i pressed the key and start aiming when i stopped.

--Client
local GS = require(script.Parent.WeaponSetup)
local Players = game:GetService('Players')
local toggleScopeRemoteEvent = game.ReplicatedStorage.Aim
local A = GS.Player.Character:FindFirstChild("Humanoid"):LoadAnimation(GS.Client.Aim)

local aiming = false

local function playerAdded(player: Player)
	local function updateScopingForPlayer(plr, isAiming)
		if aiming == true then
		A:Play()
			--GS.Client.Fire.AnimationId = GS.AimFireAnimation
			--GS.TweenService:Create(GS.Camera, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FieldOfView = 20}):Play()
			--GS.TweenService:Create(GS.Lighting.DepthOfField, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FarIntensity = 1}):Play()
		elseif aiming == false then
			A:Stop()
			--GS.Client.Fire.AnimationId = GS.FireAnimation
			--GS.TweenService:Create(GS.Camera, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FieldOfView = 120}):Play()
			--GS.TweenService:Create(GS.Lighting.DepthOfField, TweenInfo.new(0.2, Enum.EasingStyle.Sine), {FarIntensity = 0}):Play()
		end
	end

	local isAiming = player:GetAttribute("Aiming")==true
	updateScopingForPlayer(player, isAiming)

	player:GetAttributeChangedSignal("Aiming"):Connect(function()
		local isAiming = player:GetAttribute("Aiming")
		updateScopingForPlayer(player, isAiming)
	end)
end

GS.UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Q then
		aiming = true
		
		toggleScopeRemoteEvent:FireServer(aiming)
	end
end)
GS.UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Q then
		aiming = false

		toggleScopeRemoteEvent:FireServer(aiming)
	end
end)

GS.Tool.Unequipped:Connect(function()
	aiming = false
	A:Stop()
	GS.Camera.FieldOfView = 120
	GS.Lighting.DepthOfField.FarIntensity = 0
	script.Enabled = false
end)

for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
	playerAdded(player)
end

Players.PlayerAdded:Connect(playerAdded)

when it aim fire a remote event to server then play the animation in server side

Can you make this more clear please?

i dont understand your question. can you explain?

This part, can you make this more clear? (Explain better)