I have a problem with Player.Chararcter
My code:
local Players = game:GetService("Players")
local Character = Players.LocalPlayer.Character
Pls help me!
I have a problem with Player.Chararcter
My code:
local Players = game:GetService("Players")
local Character = Players.LocalPlayer.Character
Pls help me!
Try
local Character = Players.LocalPlayer:WaitForChild("Character")
local Character = Player.Character or Player.CharacterAdded:Wait()
If Player.Character
is nil, the script will yield and wait until a character is set to the player.
The character is not a child of the player, it’s an ObjectValue property.
Isn’t working:
game.Players.LocalPlayer isn’t useable in server scripts.
Your script isn’t working:
Then…I need a local script???
The best way to get the players character is using the following:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
This can only be used in a local script because I am using the LocalPlayer object. If you want to get the players character in a server script then you just use:
local Player = game.Players.supergreatr299 -- This is just an example of my username
local Character = Player.Character
You can also do this in a server script:
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
--do whatever
end)
The error is happening because if this is a server script and you are trying to get the local player which is not possible
Thank you so much!!!
the way to make it in a variable is:
local player = game:GetService("Players")
local char = player.Character or player.CharacterAdded:Wait()
-- [[ You can do anything else here ]] --
This won’t work. You’re trying to get the character from the player service, not the actual player.
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
This is the best way to do it since you wait for the player to load also. And the player object belongs to the main scope of the script.