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 make the script wait for the character to load? [Still Unanswered]

I'm making a spells script and in play solo of course the player loads before anything else unlike on a actual roblox server the scripts load before the character so when I go to play online my script doesn't work

I tried using player.CharacterAdded:wait() but It doesn't work i'm not even sure if its supposed to be used for this issue ]

heres my script :

local player = game.Players.LocalPlayer
local Player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
player.CharacterAdded:wait()
RightShoulder = Player.Character.Torso["Right Shoulder"]
Run = game:getService("RunService")
Mouse.KeyDown:connect(function(key)
	if script.Wait.Value == false then
if key == "f" then
	script.Wait.Value = true
	for i = 1, 2 do
			for i = 1, 12 do
				RightShoulder.C0 = RightShoulder.C0 *CFrame.Angles(0, 0, 0.16)
				RightShoulder.C0 = RightShoulder.C0 *CFrame.Angles(0, 0, 0)
				Run.Stepped:wait(0.01)
			end
			for i = 1, 12 do
				RightShoulder.C0 = RightShoulder.C0 *CFrame.Angles(0, 0, 0)
				RightShoulder.C0 = RightShoulder.C0 *CFrame.Angles(0, 0, -0.16)
				Run.Stepped:wait(0.005)
			end
		end
			for i = 1, 12 do
				RightShoulder.C0 = RightShoulder.C0 *CFrame.Angles(0, 0, 0)
				RightShoulder.C0 = RightShoulder.C0 *CFrame.Angles(0, 0, 0)
				Run.Stepped:wait(0.005)
			end
game:getService("Chat"):Chat(player.Character.Head, "FireBall!")
local p = Instance.new("Part", Workspace)
p.Color = Color3.new(0, 0, 0)
p.Transparency = 0.5
p.Size = Vector3.new(3, 3, 3)
p.TopSurface = "Smooth"
p.BottomSurface = "Smooth"
p.CanCollide = false
p.Shape = "Ball"
s = script.FD:Clone()
s.Parent = p
p.CFrame = Player.Character.Torso.CFrame*CFrame.new(0, 0, -12)
y = Instance.new("BodyVelocity", p)
y.maxForce = Vector3.new(math.huge, math.huge, math.huge)
y.velocity = Player.Character.Torso.CFrame.lookVector*80
local f = Instance.new("Fire", p)
wait(6)
game.Debris:addItem(p, 1)
script.Wait.Value = false
else print'waiting'
end
end
	end)

5 comments

0

Uh, why you defines 2 LocalPlayers in the script...?

0

game:IsLoaded

0

im a noob scripter but i think you could try game.Players.WaitForChild("Player")

0

bruh this is from 5 years ago why would he reply?

0

lol

20 answers

local Player = game:GetService'Players'.LocalPlayer
local character = Player.Character or Player.CharacterAdded:wait() -- in case the character model has already been created
local torso = character:WaitForChild'Torso'
local rightShoulder = torso:WaitForChild'Right Shoulder'
local head = character:WaitForChild'Head'

:WaitForChild(string childName) is your friend :P

3 comments

0

still doens't work

0

thanks dude

0

:WaitForChild() Should do the trick!

local player = game.Players.LocalPlayer

repeat
wait()
until player.Character:FindFirstChild("Torso")

My method.

I don't know if you still don't know the answer, but there's something called HasAppearanceLoaded that seems to work. Here's an example of it:

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(char)
		while player:HasAppearanceLoaded() == false do
			wait()
		end
		
		local C = char:GetChildren()
		
		for i = 1, #C do
			if C[i]:IsA("Hat") or C[i]:IsA("Accessory") then
				C[i]:Destroy()
			end
			wait(0.001)
		end
		
		local WB = game:GetService("ReplicatedStorage"):WaitForChild("Hats"):WaitForChild("WizardsBeard"):Clone()
		local WH = game:GetService("ReplicatedStorage"):WaitForChild("Hats"):WaitForChild("WizardsHat"):Clone()
		WB.Parent = char
		WH.Parent = char
	end)
end)

This is how I just did it and it worked.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

player:WaitForCharacter()

5 comments

0

where do I put this in the script?

0

now I get an error saying 22:57:45.043 - WaitForCharacter is not a valid member of Player 22:57:45.045 - Script 'Players.Player1.Backpack.Spells', Line 2

0

Hm. Try player:WaitForChild("Character"), I think WaitForCharacter isn't a thing. I'm tired and confused myself, sorry!

0

still doesn't work and now it doesn't even work in play solo

2

Try repeat wait() until player:FindFirstChild("Character")

LocalScript, obviously

local player = game.Players.LocalPlayer
local character = player:WaitForChild("Character")

1 comment

3

character isnt a child of player

Man CharacterLoaded fires when the character is loaded

local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local char = plr.Character

This is usually how I go about waiting for the character to load.

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character

Using TypeScript

	function onPlayerAdded(player: Player) {
	    player.CharacterAdded.Connect((character) => {
	        while(!player.HasAppearanceLoaded()) {
	            wait();
	        }
		// do something here...
	    })    
	}
	
	Players.PlayerAdded.Connect(player => {
	        onPlayerAdded(player);
	    }
	);
	
	Players.GetPlayers().forEach(player => {
	    onPlayerAdded(player);
	});

I think I've figured it out.

Using the workspace, you can check if the model is the playername, and if it is, it will print a success message.

game.Players.PlayerAdded:Connect(function(Player)
	workspace.ChildAdded:Connect(function(Child)
		if Child.Name == Player.Name then
			print('Character has loaded.')
		end
	end)
end)

It seems like you got a lot of answers already, but simply if I was waiting for a player to load, just use an approximate guess on how long it will take and then use that as a wait().

I like doing this:

local Character = player.Character or player.CharacterAdded:Wait()

before your script starts just put this in:

game:GetService("Players").PlayerAdded:Connect(function(plr)
game.Workspace:WaitForChild(plr.Name)
end)

if it does not work please tell me

Insert a local script, in the StarterPlayer, in StarterPlayerScripts and write the following

local Player = game.Players.LocalPlayer
repeat wait() until Player:GetCharacterFromPlayer(char)

-------------------------------------------------------------------------

char:FindFirstChild("Humanoid")
char.Humanoid.WalkSpeed = 50

You can also add a parameter so you can make more code. SIDENOTE: It's easier to get the character in a local script, than in a normal script

I'd do this. It'll work for server (regular) scripts:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	repeat
		wait()
	until
	player.Character ~= nil
end)
local char = player.CharacterAdded:Wait(5)

Hope this helps!

1 comment

0

this is a 5 year old question.

If you want it to wait for your physical character this will work in a local script:

game.Workspace:WaitForChild(game.Players.LocalPlayer.Name)

1 comment

0

HOW DID U GET 2 UPVOTES............. Do you not realize if the players name is a part in the game, you could destroy the player...

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