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.