I’m trying to load the last character the player used.
For some odd reason though, when the player selects one of the characters and rejoins, it won’t load it. But if the player chooses the other character out of the two in game so far, it does load that one.
Code:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
while true do
wait(0.1)
if Character.Parent == workspace then
local leaderstats = game.ServerStorage:FindFirstChild(player.Name)
local suits = leaderstats:FindFirstChild("Character")
if suits.Value == "Sea" then
local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
if humanoid then
local descriptionClone = humanoid:GetAppliedDescription()
descriptionClone.Shirt = "5677091404"
descriptionClone.Pants = "129458426"
descriptionClone.HairAccessory = "185812297"
humanoid:ApplyDescription(descriptionClone)
if player.Character:FindFirstChild("GirlAnimeHair_Blonde") then
player.Character:FindFirstChild("GirlAnimeHair_Blonde").Handle.Mesh.TextureId = ""
player.Character:FindFirstChild("GirlAnimeHair_Blonde").Handle.Color = Color3.new(0.333333, 0.666667, 1)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Sea"
end
end
if suits.Value == "Counter" then
local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
if humanoid then
local descriptionClone = humanoid:GetAppliedDescription()
descriptionClone.Shirt = "136506010"
descriptionClone.Pants = "136505906"
descriptionClone.HairAccessory = "376548738"
humanoid:ApplyDescription(descriptionClone)
local find = player.Character:FindFirstChild("BrownCharmerHair")
if find then
find.Handle.Mesh.TextureId = ""
find.Handle.Color = Color3.new(0.313725, 0.141176, 0.0156863)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Counter"
end
end
end
end
end)
end)
I’ve tried changing the starter value in the datastore, same result. I’ve tried to force it too, as seen above, with me setting the character value once again. For more information, my character change script does work, while also changing the value in server storage.
Character 1 (the one with issues):
Character 2 (the one that works well):
I have checked the code for mistakes, nothing. Also no errors in the output.
1 Like
As a debugging step can you add a print under the if statement for the one which is not working and see if it actually prints?
Just tested, it does print, but not load
Which one is the one which is not loading “Sea” or “Counter”
Well the counter one. It’s also the default for new players who have no value set yet
And it works once but then stops working? Is the first time being applied set somewhere else and if so can we see that code? Also instead of using two if statements I suggest using an elseif for the “Counter” as there is no need to check both.
I’ve also realized something I didn’t see the first time looking but is there any reason there is a while loop constantly setting their appearance instead of just setting it once per Character Respawn? This loop is going to duplicate every time a character respawns and can cause a pretty series memory leak.
It basically never works for the counter character. It keeps printing but nothing else. The code which changes the value originally is
if data == "counter" then
local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
if humanoid then
local descriptionClone = humanoid:GetAppliedDescription()
descriptionClone.Shirt = "136506010"
descriptionClone.Pants = "136505906"
descriptionClone.HairAccessory = "376548738"
humanoid:ApplyDescription(descriptionClone)
local find = player.Character:FindFirstChild("BrownCharmerHair")
if find then
find.Handle.Mesh.TextureId = ""
find.Handle.Color = Color3.new(0.313725, 0.141176, 0.0156863)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Counter"
end
end
data is recieved from a remote, and is set accordingly to the gui button the player pressed.
Regarding the if statement, I also used elseif, I was just testing to see if it would work this way.
And the while loop was supposed to have a break which I also removed to see if it was causing the issue. And players can’t die since it’s a lobby anyways so that’ll do. (My code isn’t the best, indeed)
No worries, I’m going to do a bit of testing and send you a code sample to try out!
Here you go. A few initial changes which I briefly documented. I want to see if this works first just as there was a few things you were doing which could of POSSIBLY caused the issue, however I kind of doubt it. Try it out and let me know.
--Added a seperate function to apply humanoid description to clear up some function clutter
function applyDescription(humanoid, descriptionInfo)
local descriptionClone = humanoid:GetAppliedDescription()
descriptionClone.Shirt = descriptionInfo.Shirt or ""
descriptionClone.Pants = descriptionInfo.Pants or ""
descriptionClone.HairAccessory = descriptionInfo.HairAccessory or ""
humanoid:ApplyDescription(descriptionClone)
end
--Removed the while loop
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
if humanoid and Character.Parent == workspace then
local leaderstats = game.ServerStorage:FindFirstChild(player.Name)
local suits = leaderstats:FindFirstChild("Character")
if suits then
--Changed the two if statements into an elseif statement
if suits.Value == "Sea" then
applyDescription({Shirt = "5677091404", Pants = "129458426", HairAccessory = "185812297"})
if player.Character:FindFirstChild("GirlAnimeHair_Blonde") then
player.Character:FindFirstChild("GirlAnimeHair_Blonde").Handle.Mesh.TextureId = ""
player.Character:FindFirstChild("GirlAnimeHair_Blonde").Handle.Color = Color3.new(0.333333, 0.666667, 1)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Sea"
elseif suits.Value == "Counter" then
applyDescription({Shirt = "136506010", Pants = "136505906", HairAccessory = "376548738"})
if player.Character:FindFirstChild("BrownCharmerHair") then
player.Character:FindFirstChild("BrownCharmerHair").Handle.Mesh.TextureId = ""
player.Character:FindFirstChild("BrownCharmerHair").Handle.Color = Color3.new(0.313725, 0.141176, 0.0156863)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Counter"
end
end
end
end)
end)
Same thing, once again, no errors.
Try this and tell me if both print during the time it DOESN’T load.
--Added a seperate function to apply humanoid description to clear up some function clutter
function applyDescription(humanoid, descriptionInfo)
local descriptionClone = humanoid:GetAppliedDescription()
descriptionClone.Shirt = descriptionInfo.Shirt or ""
descriptionClone.Pants = descriptionInfo.Pants or ""
descriptionClone.HairAccessory = descriptionInfo.HairAccessory or ""
humanoid:ApplyDescription(descriptionClone)
end
--Removed the while loop
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
if humanoid and Character.Parent == workspace then
local leaderstats = game.ServerStorage:FindFirstChild(player.Name)
local suits = leaderstats:FindFirstChild("Character")
if suits then
--Changed the two if statements into an elseif statement
if suits.Value == "Sea" then
applyDescription({Shirt = "5677091404", Pants = "129458426", HairAccessory = "185812297"})
if player.Character:FindFirstChild("GirlAnimeHair_Blonde") then
player.Character:FindFirstChild("GirlAnimeHair_Blonde").Handle.Mesh.TextureId = ""
player.Character:FindFirstChild("GirlAnimeHair_Blonde").Handle.Color = Color3.new(0.333333, 0.666667, 1)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Sea"
elseif suits.Value == "Counter" then
print("Attempting to set Humanoid Description")
applyDescription({Shirt = "136506010", Pants = "136505906", HairAccessory = "376548738"})
if player.Character:FindFirstChild("BrownCharmerHair") then
player.Character:FindFirstChild("BrownCharmerHair").Handle.Mesh.TextureId = ""
player.Character:FindFirstChild("BrownCharmerHair").Handle.Color = Color3.new(0.313725, 0.141176, 0.0156863)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Counter"
print("Set Humanoid Description!")
end
end
end
end)
end)
Absolutely nothing, including prints, weird.
Try this and tell me everything it prints (If anything)
--Added a seperate function to apply humanoid description to clear up some function clutter
function applyDescription(humanoid, descriptionInfo)
local descriptionClone = humanoid:GetAppliedDescription()
descriptionClone.Shirt = descriptionInfo.Shirt or ""
descriptionClone.Pants = descriptionInfo.Pants or ""
descriptionClone.HairAccessory = descriptionInfo.HairAccessory or ""
humanoid:ApplyDescription(descriptionClone)
end
--Removed the while loop
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
print("Character Loaded")
local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
if humanoid and Character.Parent == workspace then
local leaderstats = game.ServerStorage:FindFirstChild(player.Name)
local suits = leaderstats:FindFirstChild("Character")
if suits then
--Changed the two if statements into an elseif statement
print(suits.Value)
if suits.Value == "Sea" then
applyDescription({Shirt = "5677091404", Pants = "129458426", HairAccessory = "185812297"})
if player.Character:FindFirstChild("GirlAnimeHair_Blonde") then
player.Character:FindFirstChild("GirlAnimeHair_Blonde").Handle.Mesh.TextureId = ""
player.Character:FindFirstChild("GirlAnimeHair_Blonde").Handle.Color = Color3.new(0.333333, 0.666667, 1)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Sea"
elseif suits.Value == "Counter" then
print("Attempting to set Humanoid Description")
applyDescription({Shirt = "136506010", Pants = "136505906", HairAccessory = "376548738"})
if player.Character:FindFirstChild("BrownCharmerHair") then
player.Character:FindFirstChild("BrownCharmerHair").Handle.Mesh.TextureId = ""
player.Character:FindFirstChild("BrownCharmerHair").Handle.Color = Color3.new(0.313725, 0.141176, 0.0156863)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Counter"
print("Set Humanoid Description!")
end
end
end
end)
end)
Only the first print. Also, I noticed that on this version, none work. On the older code, the sea one worked. Okay so, I figured something out. Changing to the counter one only works if the sea one was already changed to. My best bet was changing to the sea character first and then to the counter one. Interesting enough, it worked
Try this last one
--Added a seperate function to apply humanoid description to clear up some function clutter
function applyDescription(humanoid, descriptionInfo)
local descriptionClone = humanoid:GetAppliedDescription()
descriptionClone.Shirt = descriptionInfo.Shirt or ""
descriptionClone.Pants = descriptionInfo.Pants or ""
descriptionClone.HairAccessory = descriptionInfo.HairAccessory or ""
humanoid:ApplyDescription(descriptionClone)
end
--Removed the while loop
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
print("Character Loaded")
local humanoid = Character and Character:FindFirstChild("Humanoid")
print(humanoid)
if humanoid and Character.Parent == workspace then
print("Humanoid Found and Character in workspace")
local leaderstats = game.ServerStorage:FindFirstChild(player.Name)
local suits = leaderstats:FindFirstChild("Character")
if suits then
--Changed the two if statements into an elseif statement
print(suits.Value)
if suits.Value == "Sea" then
applyDescription({Shirt = "5677091404", Pants = "129458426", HairAccessory = "185812297"})
if player.Character:FindFirstChild("GirlAnimeHair_Blonde") then
player.Character:FindFirstChild("GirlAnimeHair_Blonde").Handle.Mesh.TextureId = ""
player.Character:FindFirstChild("GirlAnimeHair_Blonde").Handle.Color = Color3.new(0.333333, 0.666667, 1)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Sea"
elseif suits.Value == "Counter" then
print("Attempting to set Humanoid Description")
applyDescription({Shirt = "136506010", Pants = "136505906", HairAccessory = "376548738"})
if player.Character:FindFirstChild("BrownCharmerHair") then
player.Character:FindFirstChild("BrownCharmerHair").Handle.Mesh.TextureId = ""
player.Character:FindFirstChild("BrownCharmerHair").Handle.Color = Color3.new(0.313725, 0.141176, 0.0156863)
end
game.ServerStorage:FindFirstChild(player.Name):FindFirstChild("Character").Value = "Counter"
print("Set Humanoid Description!")
end
end
end
end)
end)
Again tell me what all prints.
I’ve managed to solve it by loading the second and then the first over the second. Also, it seems like taking the while out breaks it.
Shouldn’t break it unless there is something else trying to set it. Also again I HIGHLY recommend to NOT have it in a while loop as the way you have it will lead to memory leaks. A single player resetting a bunch of times will basically break the game.
Also I’m not sure that solution is optimal, however in the end it is obviously up to you.
I’ve added breaks actually, and it still works, so I’m unsure what causes this. The while loop part I agree on, but at least if it’s a simple lobby without resetting enabled it would do. Thanks for the help though. 
1 Like
No problem, I wish you luck with your game!