What's wrong with this script?

It’s a pretty simple script, I’m just trying to get the player’s character from username but it doesn’t seem to be working?

image

And if you have any better ways (SIMPLE ONLY) of getting the player’s workplace character please tell me!

1 Like

A pretty nice and easy standard way of getting the character every time is:

local char = plr.Character or plr.CharacterAdded:Wait()

It doesnt guarantee something wont happen to the character like it falls off the world and gets deleted but its nice and easy

1 Like

You could (and probably should) use the CharacterAddedEvent inside your PlayerAdded event. The CharacterAdded event tracks when a character has been added to the workspace. You can also pass a parameter through the event, which is the character that was added.

1 Like

@1Urge 's Suggestion

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
		print( Player.Name .. "' has joined the game") 

	Player.CharacterAdded:Connect(Character)
		print( Character.Name .. "'s CharacterAdded") 
	end)
end)

https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded

I tried:
image
However it’s refusing to print my username and instead printing:
image

Since the player just joined the character is not existing yet, therefore its gonna be nil which produces that error. Use CharacterAdded event as suggested by the people above.

1 Like
game.Players.PlayerAdded:Connect(function(player)
    wait(1)
    local character = player.Character
    print(character.Name)

Try this. Also use character instead of char because I think char also means something else as it gets highlighted in blue. I don’t know, I’m not in studio. This was hand written code and then typed. Hope this helps.

You said you tired but your code doesn’t apply to what he suggested.

if you had your code would have looked like this

game.Players.PlayerAdded:Connect(function(a)
local char = a.Character or a.CharacterAdded:Wait()
print(char.Name)
end)
1 Like