Too many If/Else Statements Help

  1. What do you want to achieve? Keep it simple and clear!

I’m a beginner coder and I’ve noticed a pattern where I’m using a ton of If/ Else Statements in my code and was wondering if there is a better way to go about this so I don’t have 1000 lines of code for 1 argument.

What I’m trying to achieve is that every time a player dies their customized character loads in

However, I ran into an error. When a player customizes their Character and doesn’t choose any outfit. but instead goes with the default outfit provided. the SaveUserOutfits data ends up nil because nothing was chosen. So now I have to write a ton of if/else statement for every humanoidDescription line to check if the SavedUserOutfits information is nil to fix this error.

You can see on line 29 where i start the chain of if/else statements

I was wondering if there is an easier way to go about this that will make my code more optimized or will I just have to bite the bullet and write all the if statements.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)

		local humanoid = char:FindFirstChild("Humanoid")

		if hasdied == true and plr.Team == babyteam then
			wait(plr.CharacterAppearanceLoaded)

			local humanoidDescription = Instance.new("HumanoidDescription")
			humanoidDescription.Parent = plr
			humanoidDescription.BodyTypeScale = 0

			humanoidDescription.HeadScale = 0.6
			humanoidDescription.HeightScale = 0.4
			humanoidDescription.WidthScale = 0.4
			humanoidDescription.DepthScale = 0.4

			humanoidDescription.ClimbAnimation = 619521311

			humanoidDescription.Head = 86498113
			humanoidDescription.Torso = 376547767--86499666 
			humanoidDescription.LeftArm = 376547633 --86499716 
			humanoidDescription.RightArm = 376547341--86499698
			humanoidDescription.LeftLeg = 376546668--86499753
			humanoidDescription.RightLeg = 376547092--86499793

			for findUserId, outfitInfo in pairs(SaveUserOutfits) do
				if findUserId == plr.UserId then
					if outfitInfo.Face ~= nil then
						humanoidDescription.Face = outfitInfo.Face
					else
						humanoidDescription.Face = 162068415
					end
					humanoidDescription.HairAccessory = outfitInfo.Hair
					humanoidDescription.Pants = outfitInfo.Pants
					humanoidDescription.Shirt = outfitInfo.Shirt

					humanoidDescription.HeadColor = outfitInfo.SkinColor
					humanoidDescription.LeftArmColor = outfitInfo.SkinColor
					humanoidDescription.RightArmColor = outfitInfo.SkinColor
					humanoidDescription.LeftLegColor = outfitInfo.SkinColor
					humanoidDescription.RightLegColor = outfitInfo.SkinColor
					
					
				end
			end



			if humanoid then

				humanoid:ApplyDescription(humanoidDescription)

			end


			hasdied = false
		else
			print("No")
		end

		humanoid.Died:Connect(function(userid)
			hasdied = true

		end)
	end)
end)

Sorry if the question doesnt make much sense im still a beginner.
.

2 Likes

This is what the code would look like with all the if statements and I feel like there is a better way to go about it but im not sure how to do it

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)

		local humanoid = char:FindFirstChild("Humanoid")

		if hasdied == true and plr.Team == babyteam then
			wait(plr.CharacterAppearanceLoaded)

			local humanoidDescription = Instance.new("HumanoidDescription")
			humanoidDescription.Parent = plr
			humanoidDescription.BodyTypeScale = 0

			humanoidDescription.HeadScale = 0.6
			humanoidDescription.HeightScale = 0.4
			humanoidDescription.WidthScale = 0.4
			humanoidDescription.DepthScale = 0.4


			humanoidDescription.ClimbAnimation = 619521311




			humanoidDescription.Head = 86498113
			humanoidDescription.Torso = 376547767--86499666 
			humanoidDescription.LeftArm = 376547633 --86499716 
			humanoidDescription.RightArm = 376547341--86499698
			humanoidDescription.LeftLeg = 376546668--86499753
			humanoidDescription.RightLeg = 376547092--86499793

			for findUserId, outfitInfo in pairs(SaveUserOutfits) do
				if findUserId == plr.UserId then
					if outfitInfo.Face ~= nil then
						humanoidDescription.Face = outfitInfo.Face
					else
						humanoidDescription.Face = 162068415
					end

					if outfitInfo.Hair ~= nil then
						humanoidDescription.HairAccessory = outfitInfo.Hair
					else
						humanoidDescription.HairAccessory = 4772335492
					end

					if outfitInfo.Pants ~= nil then
						humanoidDescription.Pants = outfitInfo.Pants
					else
						humanoidDescription.Pants = 7083519899
					end

					if outfitInfo.Shirt ~= nil then
						humanoidDescription.Shirt = outfitInfo.Shirt
					else
						humanoidDescription.Shirt = 2170784658
					end

					if outfitInfo.SkinColor ~= nil then
						humanoidDescription.HeadColor = outfitInfo.SkinColor
						humanoidDescription.LeftArmColor = outfitInfo.SkinColor
						humanoidDescription.RightArmColor = outfitInfo.SkinColor
						humanoidDescription.LeftLegColor = outfitInfo.SkinColor
						humanoidDescription.RightLegColor = outfitInfo.SkinColor
					else
						humanoidDescription.HeadColor = Color3.new(1, 0.827451, 0.705882)
						humanoidDescription.LeftArmColor = Color3.new(1, 0.827451, 0.705882)
						humanoidDescription.RightArmColor = Color3.new(1, 0.827451, 0.705882)
						humanoidDescription.LeftLegColor = Color3.new(1, 0.827451, 0.705882)
						humanoidDescription.RightLegColor = Color3.new(1, 0.827451, 0.705882)
					end

				end
			end



			if humanoid then

				humanoid:ApplyDescription(humanoidDescription)

			end


			hasdied = false
		else
			print("No")
		end

		humanoid.Died:Connect(function(userid)
			hasdied = true

		end)
	end)
end)
1 Like

Are you looking for a table structure, a rough implementation like this? If not let us know more details.

for findUserId, outfitInfo in pairs(SaveUserOutfits) do
	if findUserId == plr.UserId then
		local defaultDescription = { -- this is a table
			Face = 162068415,
			HairAccessory = 4772335492,
			Pants = 7083519899,
			Shirt = 2170784658,
			HeadColor = Color3.new(1, 0.827451, 0.705882),
			LeftArmColor = Color3.new(1, 0.827451, 0.705882),
			RightArmColor = Color3.new(1, 0.827451, 0.705882),
			LeftLegColor = Color3.new(1, 0.827451, 0.705882),
			RightLegColor = Color3.new(1, 0.827451, 0.705882),
		}

		for key, defaultValue in pairs(defaultDescription) do
			humanoidDescription[key] = outfitInfo[key] or defaultValue -- uses first truth value, which priority is given to outfitInfo[key]
		end
		local skinColor = outfitInfo.SkinColor or defaultDescription.HeadColor
		humanoidDescription.HeadColor = skinColor
		humanoidDescription.LeftArmColor = skinColor
		humanoidDescription.RightArmColor = skinColor
		humanoidDescription.LeftLegColor = skinColor
		humanoidDescription.RightLegColor = skinColor
	end
end
2 Likes

I don’t see the problem with using those if statements. One thing, that

is gonna fire every single time that the humanoid dies, and may stack up. If you don’t want this, use humanoid.Died:Once(function(userid)
once functions the same as connect, but it automatically disconnects the statement when it fires once.

3 Likes

This is exactly what I was looking for thank you so much!

So this block of code will check to see if the user hasOutfit data saved and if it doesn’t then it will go with the default table? You seriously just saved me so much time thank you :rofl:

for key, defaultValue in pairs(defaultDescription) do
			humanoidDescription[key] = outfitInfo[key] or defaultValue -- uses first truth value, which priority is given to outfitInfo[key]
		end
1 Like

Im learning so much I didnt even know you could do humanoid.Died:Once(function(userid)

thank you!

You’re welcome, I only learned about once today as well :laughing:

1 Like

yeah you are correct, thats exactly what it does : )

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.