Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i find a certain player in workspace?

This is not a local script so i cant do local player so how would i do this?

The last function is what i need help on Playa is where i need the player to be accesed I've already tried game.Workspace.Player and it says there is no 'Player' in worksapce

game.Players.PlayerAdded:connect(function(player)
	stat = Instance.new("IntValue", player)
	stat.Name = "leaderstats"
	
	Gold = Instance.new("IntValue", stat)
	Gold.Name = "Gold"
	
	Exp = Instance.new("IntValue", stat)
	Exp.Name = "XP"
	
	Level = Instance.new("IntValue", stat)
	Level.Name = "Level"
	Level.Value = 0
	

	xpTotal = 11 * Level.Value
	
	player.leaderstats.XP.Changed:connect(function()
		
		
		if Exp.Value > 11 * Level.Value then
		Level.Value = Level.Value + 1 --add a level
		Exp.Value = Exp.Value - xpTotal --If its greater than five carry over
		
		end
		if Exp.Value == 10 * Level.Value then
			Level.Value = Level.Value + 1 -- add a level
			Exp.Value = 0 --if it = 4 then Send exp to zero
	end
	end)
	player.leaderstats.Level.Changed:connect(function()
		Playa = 
			Playa.Humanoid.MaxHealth = Level.Value * 10 + 100
			Playa.Humanoid.Health = Playa.Humanoid.MaxHealth
	end)
end)

2 answers

I suppose you mean the player's "Character model" which is located in the Workspace. A player's Player object and a player's Character model are two whole different things.

Anyway, it just so happens Player objects have a Character property that points to the player's Character model:

local character = player.Character

Note that this will return nil if the character model does not exist (It hasn't spawned yet). If you wish to wait until the character model spawns, you can do this:

local character = player.Character or player.CharacterAdded:wait()

CharacterAdded is an event that is part of Player objects. It is fired by Roblox when the player's Character model is spawned. The :wait() method simply tells Roblox to pause the execution of the script until it fires CharacterAdded. When Roblox resumes the execution of the script, :wait() will return the Character model.

To make it simple, this line means "Assign to my variable character the player's Character model (or wait for it to exist and then assign it, if it does not already).".


If you ever want to do the opposite, that is, getting a Player object from a player's Character model, there's this very neat method of the Players service called getPlayerFromCharacter:

local player = game.Players:getPlayerFromCharacter(character)

This will return nil if the character model is not that of a player.

Answer edited because it is not valid.

Hehe, I realized the error of my ways...

--Oops

2 comments

0

This is a bad idea. A model could easily have the same name as a player. Plus, you should only use WaitForChild() when there is a chance for the object not to exist just yet.findFirstChild().

0

Okay, I see what you mean here. I am going to edit my post so that it doesn't give the answer, just says "Answer edited because it is not valid."

This discussion is preserved as it was. New answers, comments, and votes are closed.