Need help making character creator script more simple

So, I have begun to make a character customization system for my game. The problem is that I feel like I could make it more simple/clean. Below is the start I have; any help would be appreciated!

local DSS = game:GetService("DataStoreService"):GetDataStore("DemonSlayer")

local ServerScriptService = game:GetService("ServerScriptService")

local hNext = game.ReplicatedStorage:WaitForChild("hNext")
local hBack = game.ReplicatedStorage:WaitForChild("hBack")



game.Players.PlayerAdded:Connect(function(player)
	
	
	--Hair

	hNext.OnServerEvent:Connect(function(player)		
			print("test")
		player:WaitForChild("Data"):WaitForChild("Hair").Value = player:WaitForChild("Data"):WaitForChild("Hair").Value + 1
		
		
		---------------------------------------------------------------------
		
		if player:WaitForChild("Data"):WaitForChild("Hair").Value == 0 then



			local hair = game.ServerScriptService.Hair.Hair2:Clone()
			hair.Parent = player.Character

		end	
		
		
		
		if player:WaitForChild("Data"):WaitForChild("Hair").Value == 1 then
			
			
			
			local hair = game.ServerScriptService.Hair.Hair1:Clone()
			hair.Parent = player.Character
			
	end	
end)
	
	
	
	
	hBack.OnServerEvent:Connect(function(player)		
		print("test")
		player:WaitForChild("Data"):WaitForChild("Hair").Value = player:WaitForChild("Data"):WaitForChild("Hair").Value - 1
		
		
	
	end)
end)


Since it looks like your naming scheme for the Hair object is consistent with the number.

You could create a string with the data you pull and use FindFirstChild.

local HairValue = player.Data.Hair.Value
local HairObject = game.ServerScriptService.Hair:FindFirstChild("Hair"..tostring(HairValue))
print(HairObject)

Give that a shot.

1 Like

Ah that works a lot better! So, would I then just clone the hair to the player with that same system?

Yeah, you would just do HairObject:Clone().Parent = player.Character

1 Like

Thank you! The last thing I’d like to ask is how would I remove the previous hair the player selected if they change their hair?

You would just reference the current data before changing it.

local HairValue = player.Data.Hair.Value
local PlayersHair = player.Character:FindFirstChild("Hair"..tostring(HairValue))
PlayersHair:Destroy()

If you add that within’ the hBack RemoteEvent before you change the Hair value it’ll remove the previous hair.

Also I would recommend moving the RemoteEvent stuff outside of PlayerAdded or it’s going to cause complications with multiple players.

1 Like