#Player and Character
1 messages · Page 1 of 1 (latest)
Heavily depends on what script you're in, and what you need the player or character for
In a local script, you can easily get the player by doing
local player = game.Players.LocalPlayer
You can get the character from the player by doing
local character = player.Character
and you can do this the other way around, get the player from the character object
local playerCharacter = -- the player's character
local player = game.Players:GetPlayerFromCharacter(playerCharacter)
There are also a bunch of different events that can give you either, but it depends on the context of your script
What about for a server script, for example I’m making a tool and I just need a variable for the player to simplify everything
if you are using a remote event / function the player object is given as a parameter, or if the script is inside of the tool, which is a descendant of the player, then you can just directly reference the player from there
If you want to show the script you're working on, I can give you somebetter examples that fit your situation
This is my script for the tool right now
local tool = script.Parent
local character = nil
local player = nil
tool.Equipped:Connect(function()
task.wait(3)
character = tool.Parent
player = game.Players:GetPlayerFromCharacter(character)
if player then
print("Tool equipped by", player.Name)
else
warn("Could not find player from character.")
end
end)
it's in a server script
Then yeah that's a good exaple, though be aware that a tool is typically stored in the player's backpack, not the character itself
ill have to test out if it still works while just as a child to the character
Yeah but once it’s equipped the parent becomes the character, so my idea was to get the character after the tool ins equiped
I see, then yeah that looks good, does it work?
Nope
try printing both the character and player to see what it is, also let me know whats going on in the console
Nothing appears in the output
then that means the equipped event isnt running, make sure the script is enabled and is in a place where it can run, and make sure you are equipping the right tool with the prints in it