Why doesn't code run in CharacteAdded on Play Solo mode?

Hi, so I have code that basically changes the appearance of the player and teleports them as soon as they join but this code works 2/3 times, the other 1/3 time it doesn’t execute any code. Is it because the player is still loading into the client or something? By the way, when testing this part of code IN-GAME, it works FINE but in play solo mode it doesn’t do anything to the player 33% of the time.
Here is some of my code:

Player.CharacterAdded:Connect(function(character)

--wait(0.5)
	
print("Waiting")
	
--local character = Player.Character
--if not character or not character.Parent then
--	character = Player.CharacterAdded:wait()
--end
	
wait()

– if not Player.Character then Player.CharacterAdded:Wait() end

--Player.CharacterAdded:Wait()
	
print("Waiting done")
	
character:WaitForChild("Humanoid").NameDisplayDistance = 0
	
--wait(0.1)
	
print("Adding armor value now to humanoid.")
	
--repeat wait()	
--until character.Parent == workspace
	
--character:WaitForChild("HumanoidRootPart"):SetNetworkOwner(nil)

local armor = Instance.new("IntValue", character)
armor.Name = "ArmorValue"
armor.Value = 0
	
wait()
	
local foundShirt = false
local foundPants = false
	
for i, v in ipairs(character:GetChildren()) do
	if v:IsA("Accessory") then
		print("Found accessory!")
		v:Remove()
	elseif v:IsA("Shirt") then
		foundShirt = true	
		v.ShirtTemplate = "http://www.roblox.com/asset/?id=5829421594"
	elseif v:IsA("Pants") then
		foundPants = true
		v.PantsTemplate = "http://www.roblox.com/asset/?id=5829420444"
	else
		print("This is not an accessory. The item name: " .. v.Name)	
	end
end
	
for i, v in ipairs(character.Head:GetChildren()) do
	if v:IsA("Decal") then
		print("Found face!")
		v.Texture = "http://www.roblox.com/asset/?id=144080495"
		--v:Remove()
	elseif v:IsA("SpecialMesh") then
			--v.MeshType = "Head"
		print("Special mesh found. Changing!")
			
		wait(0.7)
			
		v.MeshType = Enum.MeshType.Head
		v.Scale = Vector3.new(1.25, 1.25, 1.25)
	end
end
	
if foundShirt == false then
	local shirt = Instance.new("Shirt")	
	shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=5829421594"
	shirt.Parent = character
elseif foundPants == false then
	local pants = Instance.new("Pants")	
	pants.PantsTemplate = "http://www.roblox.com/asset/?id=5829420444"
	pants.Parent = character	
end

etc.

1 Like

The line should fire properly. Only hinderance is when the code is yielding long enough before the event even fired or the script being added too late. What a bummer.

Player.CharacterAdded:Connect(function(character)

I cut the code down off the redundancy to the issue.

Player.CharacterAdded:Connect(function(character)
	print("CharacterAdded fired, if this line doesn't show, the function simply wasn't connected in time.")
	-- Redundant lines cleared, unless they were required
	character:WaitForChild("Humanoid").NameDisplayDistance = 0 -- Default settings can be changed without altering them from the script

	local armor = Instance.new("IntValue")
	armor.Name = "ArmorValue"
	armor.Value = 0
	armor.Parent = character
	
	-- Rest of the code truncated due to redundancy to the issue
end

Hypothetically, the script was maybe parented to character or player scripts. If that was the case, the script sometimes fail because the ordering is apparently asynchronous for some reason. Meaning some kind of an approximation of race condition.

Play Solo always seem to perform differently from the accurate game scenario. The ordering is much faster and perhaps the event wasn’t connecting fast enough. This issue may be encountered in real games though.

I see- but I still don’t have a solution as your code i’ve already tried before and it doesn’t work. :confused:

I was looking for an answer to this: Where was the script parented to?

For future references, please include all the details around the “picture” that might help. Sometimes they are critical to finding the issue.

The script is just parented to workspace, it’s a standard PlayerAdded, etc. script. Thanks

Try parenting to ServerScriptService instead and see if that works there instead.

How about you take the code that changes clothes and put it in StarterCharacterScripts?
It does the same, just make sure to make it delete when it’s done.

Well I need the code to run every time a person spawns so i’ll have to put it under PlayerScripts but that means rewriting my code as it references to “wrong” variables. Will it work are you sure?

If you are in StarterCharacterScripts, every time the player reappears the script will be activated.
Just make sure you don’t have it inside a function.

Are you sure? It says here on the Roblox developer wiki:

“Unlike scripts stored in the PlayerScripts folder, these scripts will not persist when the player respawns.”

Does anyone know if this is true? Thanks

I’m not saying PlayerScripts, this is StarterCharacterScripts:
image

Create a normal(or server) script, put this code and put the script in StarterCharacterScripts.

Code
character = script.Parent
character:WaitForChild("Humanoid").NameDisplayDistance = 0
--wait(0.1)
print("Adding armor value now to humanoid.")
--character:WaitForChild("HumanoidRootPart"):SetNetworkOwner(nil)
local armor = Instance.new("IntValue", character)
armor.Name = "ArmorValue"
armor.Value = 0
wait()
local foundShirt = false
local foundPants = false
for i, v in ipairs(character:GetChildren()) do
	if v:IsA("Accessory") then
		print("Found accessory!")
		v:Destroy()
	elseif v:IsA("Shirt") then
		foundShirt = true	
		v.ShirtTemplate = "http://www.roblox.com/asset/?id=5829421594"
	elseif v:IsA("Pants") then
		foundPants = true
		v.PantsTemplate = "http://www.roblox.com/asset/?id=5829420444"
	else
		print("This is not an accessory. The item name: " .. v.Name)	
	end
end

for i, v in ipairs(character.Head:GetChildren()) do
	if v:IsA("Decal") then
		print("Found face!")
		v.Texture = "http://www.roblox.com/asset/?id=144080495"
		--v:Destroy()
	elseif v:IsA("SpecialMesh") then
		--v.MeshType = "Head"
		print("Special mesh found. Changing!")
		wait(0.7)
		v.MeshType = Enum.MeshType.Head
		v.Scale = Vector3.new(1.25, 1.25, 1.25)
	end
end

if foundShirt == false then
	local shirt = Instance.new("Shirt")	
	shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=5829421594"
	shirt.Parent = character
elseif foundPants == false then
	local pants = Instance.new("Pants")	
	pants.PantsTemplate = "http://www.roblox.com/asset/?id=5829420444"
	pants.Parent = character	
end
wait()
script:Destroy()

Also, Remove is deprecated.

This item is deprecated in favor of Instance:Destroy and Instance:ClearAllChildren . If you must remove an object from the game, and wish to use the object later, set its Parent property to nil instead of using this method.

No, it says in StarterCharacterScripts, not PlayerScripts that: " “Unlike scripts stored in the PlayerScripts folder, these scripts will not persist when the player respawns.” You can find it in the link I gave to it. Not PlayerScripts, it is talking about StarterCharacterScripts not being able to persist.

Also Remove may be deprecated but it still works.

The idea of this StarterCharacterScripts is to put it back in the player, so you don’t need a function to start the code.