LoadCharacter (Method): Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Camoy
added
 
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">"
 
(30 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{Locked}}
 
{|
<onlyinclude>{{Method
|<onlyinclude>{{Method|name = LoadCharacter
|name = LoadCharacter
|arguments = [[boolean]] '''inGame'''
|arguments = [[bool]] <var>inGame</var> = true
|returns = [[nil]]
|description = Loads in a new character for this player. This will replace the player's current character, if they have one.
|description = Will load the character, used is resurrection.  The argument is not necessary for this operation. Passing it false will load the character without animation
|object = Player
|object = [[Player]]
}}</onlyinclude>
|}}
 
</onlyinclude>
{{clear floats}}
|}
 
==Remarks==
 
LoadCharacter should be used in conjunction with [[Players]].[[CharacterAutoLoads]] to control spawning of characters. This method only works from a server-side [[Script]] (''not'' a [[LocalScript]]).
 
<!-- this is really weird -->
If <var>inGame</var> is {{true}}, then a default character will be loaded, then the Player's [[CharacterAppearance]] will be applied.
 
If <var>inGame</var> is {{false}}, then a character with the most recently loaded CharacterAppearance is used.
 
==Example==
 
This script turns off auto-loading and simulates character respawning.
 
<syntaxhighlight lang="lua">
local respawnTime = 5
 
local Players = Game:GetService("Players")
Players.CharacterAutoLoads = false
 
Players.PlayerAdded:connect(function(Player)
Player.CharacterAdded:connect(function(Character)
-- find the humanoid, and detect when it dies
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
Humanoid.Died:connect(function()
-- delay, then respawn the character
wait(respawnTime)
Player:LoadCharacter()
end)
end
end)
Player:LoadCharacter() -- load the character for the first time
end)
</syntaxhighlight>
 
[[Category:Methods]]

Latest revision as of 06:19, 27 April 2023

LoadCharacter( bool inGame = true )
Returns nil
Description: Loads in a new character for this player. This will replace the player's current character, if they have one.
Member of: Player


Remarks

LoadCharacter should be used in conjunction with Players.CharacterAutoLoads to control spawning of characters. This method only works from a server-side Script (not a LocalScript).

If inGame is true, then a default character will be loaded, then the Player's CharacterAppearance will be applied.

If inGame is false, then a character with the most recently loaded CharacterAppearance is used.

Example

This script turns off auto-loading and simulates character respawning.

local respawnTime = 5

local Players = Game:GetService("Players")
Players.CharacterAutoLoads = false

Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(function(Character)
		-- find the humanoid, and detect when it dies
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid.Died:connect(function()
				-- delay, then respawn the character
				wait(respawnTime)
				Player:LoadCharacter()
			end)
		end
	end)
	
	Player:LoadCharacter() -- load the character for the first time
end)