Custom character script is inefficient?

Hello. I have a script that transforms a player’s character into a custom one whenever a new round begins and whenever it ends, it transforms the character back to the player’s original one. The script works fine, however I do not think it is efficient enough and could break at some point.

Script:

local eradicator

local function recreateChar(eradicator)
	eradicator:LoadCharacter()
	eradicator.Character.Parent = workspace
end




local function LoadChar(Player, DesiredSpawnPosition, CharName)
	repeat wait() until Player.Character
	
	spawn(function()
		
		roundbegun.Changed:Connect(function()
			if roundbegun.Value == true then
				for _, player in pairs(game.Players:GetPlayers()) do
					if player:FindFirstChild("eradicatorvalue") then
						eradicator = player
						eradicator.Character.Humanoid.Health = 0
					end
				end
				wait(0.5)
		

		local body = game.ServerStorage.CustomCharacter:Clone()
		body.Parent = workspace

		
		body:SetPrimaryPartCFrame(CFrame.new(DesiredSpawnPosition))

		
		body.Name = eradicator.Name

		
		if eradicator.Character then
			eradicator.Character:Destroy()
		end

		
		eradicator.Character = body
				
		
		
		for i, v in pairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do
			v:Clone().Parent = body
		end

		

		
				game.ReplicatedStorage.CharacterAdded:Fire(eradicator.Character)
				
				inround.Changed:Connect(function()
					if inround.Value == false then
						eradicator:LoadCharacter()
					end
				end)
			end
		end)
	end)
end

game.Players.PlayerAdded:Connect(function(Player)
	
	LoadChar(Player, Vector3.new(0,50,0), "Eradicator")
	print("connected ".. Player.Name .. "to the loadcharacter function")

	
	game.ReplicatedStorage.CharacterAdded.Event:Connect(function(Character)
		
		print("CharAdded")

	end)
	game.ReplicatedStorage.CharacterAdded.Event:Connect(function(Character)

		local Humanoid = Character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
			wait(3)
			print("connected ".. eradicator.Name .. " to humanoid died function")
			LoadChar(eradicator,  Vector3.new(0,50,0), "Eradicator")
			if inround.Value == false then
				recreateChar(eradicator)
			end
			print("run ok")
		end)
	end)
end)

If there is a better way to write this code, let me know.

1 Like

Really bro?

local char = Player.Character or Player.CharacterAdded:Wait()

Considered using task.spawn? Also I think you should read this thread.

You also need to start using more variables.

1 Like

Along with @commitblue’s suggestions, you should use GetService when referencing services like Players because the name’s of service’s can change and it’s better for maintainability, eg:

local Players = game:GetService("Players")

You should also use task.wait(n) instead of wait(), here’s why:

And last but not least, use :PivotTo(CFrame) instead of :SetPrimaryPartCFrame(CFrame), as SetPrimaryPartCFrame has been superseded by :PivotTo.

1 Like

Thank you, I will make sure that I am aware about your suggestions. Is there anything else that I should change?

1 Like

Make it Pivotto.

Make this ipairs.