#Player and Character

1 messages · Page 1 of 1 (latest)

rich wren
#

I’m pretty new to Lua and Roblox studios, and I’ve noticed that there’s no easy way to directly get the Player or Character. Is there an easy way to save Player and Character in a variable to use in the future?

teal jacinth
#

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

rich wren
#

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

teal jacinth
#

If you want to show the script you're working on, I can give you somebetter examples that fit your situation

rich wren
#

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

teal jacinth
#

ill have to test out if it still works while just as a child to the character

rich wren
#

Yeah but once it’s equipped the parent becomes the character, so my idea was to get the character after the tool ins equiped

teal jacinth
rich wren
#

Nope

teal jacinth
# rich wren Nope

try printing both the character and player to see what it is, also let me know whats going on in the console

rich wren
#

Nothing appears in the output

teal jacinth
# rich wren 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