How to get character from ModuleScript?

Hi everyone, so I’m trying to get the player’s character from a Module script with
this code:

local Players = game:GetService("Players")
local localPlr = Players.LocalPlayer
local plrChar = localPlr.Character or localPlr.CharacterAdded:Wait()

But runs into an error saying “attempt to index nil with ‘Character’”.
How do I fix this?

2 Likes

is a LocalScript requiring the ModuleScript or a server script?
For Players.LocalPlayer to work it has to be required with a LocalScript.

2 Likes

So how would I get this information from a server script?

You could use Players.PlayerAdded

1 Like

How would I do that? I don’t know to exact syntax to do this

The link I provided shows you exactly how to get the player when they join?..
Are you trying to create a function in a module that handles a specific target player?

If you want to get a player’s character from a server script, you’ll have to find the player’s instance in game.Players instead of using Players.LocalPlayer, as the server holds all the players at once and doesn’t have one specific player it controls.

Something like this should function:

local Players = game:GetService("Players")

function GetPlayersCharacter(playername)
   local TargetCharacter
   local TargetPlayer = Players:FindFirstChild(playername)
   if TargetPlayer ~= nil then
       TargetCharacter = TargetPlayer.Character
   end
return TargetCharacter
end

You’ll have to set up a function like this in your modulescript though, and check to make sure a player’s character model exists before proceeding to carry out any further execution in any other script you call this function from.

Sorry If I Was Late But As @GetGlobals said, You Can Use PlayerAdded

example

local Module = {}

Module.StartUp = function()
    local Player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait() -- waits for player
--add your code
end