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 :
01 | local player = game.Players.LocalPlayer |
02 | local Player = game.Players.LocalPlayer |
03 | local Mouse = player:GetMouse() |
04 | player.CharacterAdded:wait() |
05 | RightShoulder = Player.Character.Torso [ "Right Shoulder" ] |
06 | Run = game:getService( "RunService" ) |
07 | Mouse.KeyDown:connect( function (key) |
08 | if script.Wait.Value = = false then |
09 | if key = = "f" then |
10 | script.Wait.Value = true |
11 | for i = 1 , 2 do |
12 | for i = 1 , 12 do |
13 | RightShoulder.C 0 = RightShoulder.C 0 *CFrame.Angles( 0 , 0 , 0.16 ) |
14 | RightShoulder.C 0 = RightShoulder.C 0 *CFrame.Angles( 0 , 0 , 0 ) |
15 | Run.Stepped:wait( 0.01 ) |
This question has been closed because it is a discussion about a topic focused on diverse opinions, which isn't a good fit for our Q&A format.
Why was this question closed?
1 | local Player = game:GetService 'Players' .LocalPlayer |
2 | local character = Player.Character or Player.CharacterAdded:wait() -- in case the character model has already been created |
3 | local torso = character:WaitForChild 'Torso' |
4 | local rightShoulder = torso:WaitForChild 'Right Shoulder' |
5 | local head = character:WaitForChild 'Head' |
:WaitForChild(string childName) is your friend :P
1 | local player = game.Players.LocalPlayer |
2 |
3 | repeat |
4 | wait() |
5 | 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:
01 | game.Players.PlayerAdded:connect( function (player) |
02 | player.CharacterAdded:connect( function (char) |
03 | while player:HasAppearanceLoaded() = = false do |
04 | wait() |
05 | end |
06 |
07 | local C = char:GetChildren() |
08 |
09 | for i = 1 , #C do |
10 | if C [ i ] :IsA( "Hat" ) or C [ i ] :IsA( "Accessory" ) then |
11 | C [ i ] :Destroy() |
12 | end |
13 | wait( 0.001 ) |
14 | end |
15 |
This is how I just did it and it worked.
1 | local Player = game.Players.LocalPlayer |
2 | local Character = Player.Character or Player.CharacterAdded:Wait() |
1 | player:WaitForCharacter() |
LocalScript, obviously
1 | local player = game.Players.LocalPlayer |
2 | local character = player:WaitForChild( "Character" ) |
1 | local plr = game.Players.LocalPlayer |
2 | repeat wait() until plr.Character |
3 | local char = plr.Character |
This is usually how I go about waiting for the character to load.
1 | local Player = game.Players.LocalPlayer |
2 | repeat wait() until Player.Character |
Using TypeScript
01 | function onPlayerAdded(player: Player) { |
02 | player.CharacterAdded.Connect((character) = > { |
03 | while (!player.HasAppearanceLoaded()) { |
04 | wait(); |
05 | } |
06 | // do something here... |
07 | } ) |
08 | } |
09 |
10 | Players.PlayerAdded.Connect(player = > { |
11 | onPlayerAdded(player); |
12 | } |
13 | ); |
14 |
15 | Players.GetPlayers().forEach(player = > { |
16 | onPlayerAdded(player); |
17 | } ); |
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.
1 | game.Players.PlayerAdded:Connect( function (Player) |
2 | workspace.ChildAdded:Connect( function (Child) |
3 | if Child.Name = = Player.Name then |
4 | print ( 'Character has loaded.' ) |
5 | end |
6 | end ) |
7 | 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:
1 | local Character = player.Character or player.CharacterAdded:Wait() |
before your script starts just put this in:
1 | game:GetService( "Players" ).PlayerAdded:Connect( function (plr) |
2 | game.Workspace:WaitForChild(plr.Name) |
3 | end ) |
if it does not work please tell me
Insert a local script, in the StarterPlayer, in StarterPlayerScripts and write the following
1 | local Player = game.Players.LocalPlayer |
2 | repeat wait() until Player:GetCharacterFromPlayer(char) |
3 |
4 | ------------------------------------------------------------------------- |
5 |
6 | char:FindFirstChild( "Humanoid" ) |
7 | 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:
1 | local Players = game:GetService( "Players" ) |
2 |
3 | Players.PlayerAdded:Connect( function (player) |
4 | repeat |
5 | wait() |
6 | until |
7 | player.Character ~ = nil |
8 | end ) |
1 | local char = player.CharacterAdded:Wait( 5 ) |
Hope this helps!
If you want it to wait for your physical character this will work in a local script:
1 | game.Workspace:WaitForChild(game.Players.LocalPlayer.Name) |