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
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.
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.
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()
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.