Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

CharacterAdded Doesn't Always Work Why Not? Does It Matter If Client Or Server Script?

Asked by 5 years ago
Edited 5 years ago

Why doesn't character added work when the character is fully added? i dont get this, and its really messing up my custom character datastore save between games.

I found a fix, was to wait five seconds, then change character values. But if i dont have that, it sometimes doesn't work at all and ignores my other code in it.

Here is my code.

local PlrsFolders = game.ReplicatedStorage:WaitForChild("Profiles")
local selectface = game.ReplicatedStorage.Database.Items.Faces
local newhairboy = game.ReplicatedStorage.Database.Items.Hair.Hair.Boys
local newhairgirl = game.ReplicatedStorage.Database.Items.Hair.Hair.Girls

game:GetService("Players").PlayerAdded:connect(function(p)
p.CharacterAdded:connect(function(char)
wait(5)
if PlrsFolders[p.UserId].Char_Info.Bodyclr.Value == 1 then
char["Body Colors"].TorsoColor = BrickColor.new("Pastel brown")
char["Body Colors"].HeadColor = BrickColor.new("Pastel brown")
char["Body Colors"].RightArmColor = BrickColor.new("Pastel brown")
char["Body Colors"].LeftArmColor = BrickColor.new("Pastel brown")
char["Body Colors"].RightLegColor = BrickColor.new("Pastel brown")
char["Body Colors"].LeftLegColor = BrickColor.new("Pastel brown")
end
  end)
end)

It changes body color for different "Bodyclr" values.

Why does this work, but if i remove wait(5)

it wont?

How can i make sure the character loads in, what if it takes more than five seconds? and it doesn't load at all? what do i do to make it accurate?

Thank you.

0
CharacterAdded must be from the server. Hope this helps! Kircic 0 — 5y
0
Thank you, this script is a server script in serverscriptservice. JuuzouBlitz 75 — 5y
0
Do wait() instead of wait(5) JayShepherdMD 147 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

The most likely reason is that you don't use WaitForChild on line 9. I assume that the information you are accessing is set up by a different script that hasn't always had the chance to run before your function is activated.

Depending on exactly what isn't there and how it adds the children, you might need WaitForChild in more than one place. In the most extreme case:

if PlrsFolders:WaitForChild(p.UserId):WaitForChild("Char_Info"):WaitForChild("Bodyclr").Value == 1 then

Further notes: - The character model has already been created when the event fires (so you don't need to wait for it to "load in"). This is separate from the client having fully loaded their character, but it isn't something to be concerned about. - Unlike what Kircic says, CharacterAdded can be used on the client, but since you're doing something for all players, this indeed should be a server script (which you say it is).

0
Thanks big help JuuzouBlitz 75 — 5y
Ad

Answer this question