Getting the character

I’ve been trying to get into scripting for very long, and now i have an enourmous amount of time to try 'n learn… but there’s one thing that has been bothering me even after having scripted some small stuff: LocalPlayer and especially the Character.

I want to know how to get the character of a player from a server script and also a local script…

I tried various things, even checked the roblox dev site…

17 Likes

to get the character of the player. It’s quite simple
(first official solution reply so I’m thrilled)
add “Character” behind the player so…

game.Players:PlayerAdded:Connect(function(plr)
local character = plr.Character
end)
1 Like

To get the LocalPlayer you need do this:

local locplr = game.Players.LocalPlayer --- (locplr you can change to other word)

after that you can do this:

locplr.leaderstats.Cash.Value -- if you making simulator, you need to have Leaderstats Script in ServerScriptService (also you can add DataStore)

If you use a function, it only applies in the function itself, no? I think i’ve tried something like that a couple times, so are you sure the defined character is also useable outside that function?

You need write

1.game.Players:PlayerAdded:Connect(function(plr)

always at start, but if you has

1. local 1 = game.Players.LocalPlayer -- not recommend (change 1 to locplr or other)

then you need write it after this

1. local 1 = game.Players.LocalPlayer -- not recommend (change 1 to locplr or other)
2.
3. game.Players.PlayerAdded:Connect(function(plr)

also (plr) you can change to (player) and more

1 Like

To get a player’s character, you can index the Character property of the player’s Player object, as such

local Character = Player.Character

Just getting the player’s character isn’t the best option here, as it may take time to load in. This is why you should use the CharacterAdded event.

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        -- do character stuff
    end)
end)

You don’t have to necessarily connect to it, as waiting for it allows for cleaner code

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
    local Character = Player.Character or Player.CharacterAdded:Wait()
    -- do character stuff
end)
3 Likes

Variable names can contain numbers, but I don’t think numbers can prefix a variable. Try changing it to LocalPlayer or Player1

1 Like

For local Scripts you can get the Character by doing

local player = game.Players.LocalPlayer
local Character = player.Character

for Server Scripts, I normally Use Remote Events to Get the Exact Player as the First Parameter is Always the Player when firing to the Server. And it makes sure any Scripting Happens on the Server not the Client

Although these replies are correct, none of them gave you the precaution that the character doesn’t load before you define the variable.

For local scripts, it’s pretty simple if you want to get the character:

local Character = game.Players.LocalPlayer.Character

Now that code only works sometimes, because sometimes the character doesn’t load fast and the variable wil be nil instead of the Character. To counteract this, you use an Added event:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() 
--[[ Notice the Player.CharacterAdded:Wait(), 
     this line will ensure it will wait for the
     character to load before defining it

Now you might of seen some people do this:

local Character = player.Character
if not Character then
    repeat wait() until player.Character
    Character = player.Character
end

That is bad practice and shouldn’t be used.


Now for server scripts, there are many ways to getting that character, keep in mind that this can be used in LocalScripts as well, but you shouldn’t, unless you have to, because the method above is more efficient.
Here are the different ways:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
    local Character = Player.Character or Player.CharacterAdded:Wait()
end)

Or

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)

    end)
end)
15 Likes

In order to get the character on a local script, simply do:

local character = game.Players.LocalPlayer.Character

On a server script, you can either use remote events or as OptimisticSide said, do:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local character = player.Character or player.CharacterAdded:Wait()
	end)
end)

You can’t use the character variable outside that function because you’ll end up needing to use the player.CharacterAdded function to accompany it.

Edit: As Crundee mentioned above, you should use player.CharacterAdded:Wait() in a local script as well.

2 Likes

I want to thank all of you! I know it’s something simple and small, so i’m grateful for still helping me out!

4 Likes