Player.Character is always = to nil

Not much to ray here except I have a function were I equip my gun, and it checks for the player’s character and it just does an infinite wait from there.
Not sure if its an engine bug or a scripting bug, because it only cant see it on the server.

1 Like

Are you being specific about which player character you are checking for? Remember that there is no LocalPlayer on the server.

If you’re trying to access the player via a local script inside of the tool then just do

local player = game.Players.LocalPlayer
local char
	
gun.Equipped:Connect(function()
	gun.Activated:Connect(function()
		char = script.Parent.Parent
	end)
end)
3 Likes

Can you be more specific and include more information such as your script. There can be many mistakes possible. I’m assuming the problem that you’re facing is that you wanna use :WaitForChild(" ") on the player and wait for the character to load, which is kind of wrong because the character isn’t a child of player. So what you can do is do player.CharacterAdded:Wait(), which will yield until the character load.
We might as well do player.Character as another option, because there might be a chance that the character actually loaded.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

Another option is what @ValueKing did! And he specifically did it inside of the .Activated event because, considering that the script is inside of the tool, once the tool is equipped, it gets re-parented to the character so the character becomes its parent.

6 Likes

I know there is no LocalPlayer on the server i can show you guys the specific function:
server module

function GunService.Client.Equip(player, tool)
	
	repeat wait() print(player.Character, workspace.HDWC) until player.Character ~= nil
	
	player.Character.Humanoid:EquipTool(tool)
	GunService:FireAllClientsd(ON_TOOLCHANGED_CLIENT_EVENT, player, tool)
	
end

local module

function Guntest:Equip(tool, viewmodel)
	repeat wait() until game.Players.LocalPlayer.Character
	gun = self.Modules.Gun.new(viewmodel, self.Services.GunService)
	self.Services.GunService:Equip(tool)
end

Make sure that ‘player’ in the server module is infact a player object. If you know for sure that your character exists upon the server calling the function then there’s no doubt that the ‘player’ variable is pointing to something else.

This is usually the method used to get the character. If you prefer running a wait loop for whatever reason, switch it to a while loop instead to avoid the first wait() call you would get in your current loop.

1 Like

You should be using Player.CharacterAdded:wait() instead of an indefinite repeat loop that hurts performance and is not always reliable. This way you will know that you’re dealing with a player that can spawn so in theory your code should not hang.

2 Likes