Gun System Connecting player from Local to Server

Hello, so I am working on a gun system. I have one event when the player equips a tool the server receives an event. I also have a local function that manages animations in the server script, here is the code for the server script:

–Main Locals–

local gameSettings = require(game.ReplicatedStorage.Modules.gameSettings)

local remote = game.ReplicatedStorage.Remotes.weaponEvents

local shootEvent = remote.shootEvent

local reloadEvent = remote.reloadEvent

local equipEvent = remote.equipEvent

local unequipEvent = remote.unequipEvent

–playAnimation Function

local function playAnimation(player)

local animator = player.character.Humanoid:WaitForChild(“Animator”)

local animationInstance = Instance.new(“Animation”)

animationInstance.AnimationId = animationName

local animation = animator:LoadAnimation(animationInstance)

animation:Play()

wait(animation.Length)

animationInstance:Destroy()

end

equipEvent.OnServerEvent:Connect(function(player)

animationName = gameSettings.glockEquip

playAnimation()

end)

The errors says:

ServerScriptService.weaponServerHandler:12: attempt to index nil with ‘character’

This is due to the line

local animator = player.character.Humanoid:WaitForChild(“Animator”)

I understand why it does not work but how would I fix this? It is since it is not seeing a player as the server script.

local gameSettings = require(game.ReplicatedStorage.Modules.gameSettings)

local remote = game.ReplicatedStorage.Remotes.weaponEvents

local shootEvent = remote.shootEvent

local reloadEvent = remote.reloadEvent

local equipEvent = remote.equipEvent

local unequipEvent = remote.unequipEvent

local function playAnimation(player, animationName)
	if player.Character then
		local animator = player.Character:WaitForChild("Humanoid"):WaitForChild("Animator")

		local animationInstance = Instance.new("Animation")

		animationInstance.AnimationId = animationName

		local animation = animator:LoadAnimation(animationInstance)

		animation:Play()

		wait(animation.Length)

		animationInstance:Destroy()
	end
end

equipEvent.OnServerEvent:Connect(function(player)
	playAnimation(player, gameSettings.glockEquip)
end)

works thank you very much very helpful

1 Like