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…
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?
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)
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)
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.