Need help with a character spawning script

As you can tell by the title, I need help with making a custom character spawning script, it works normally when you try to spawn in the first time, but when you try to respawn, it behaves very weirdly: as shown in the video
2020-06-15 16-30-36|video

This is the code:

local playerRemote = script.Parent:WaitForChild("Player")
local serverStorage = game.ServerStorage
local player = playerRemote.OnServerEvent:Wait()

local replicatedStorage = game.ReplicatedStorage
local Characters = replicatedStorage:WaitForChild("Characters")
local SetSubject = replicatedStorage:WaitForChild("SetSubject")
local loadCharacterRemote = script.Parent:WaitForChild("LoadCharacter")
local heroMissionGui = game.ServerStorage:WaitForChild("UI"):WaitForChild("HeroMissionGui")
local villainMissionGui = game.ServerStorage:WaitForChild("UI"):WaitForChild("VillainMissionGui")

loadCharacterRemote.OnServerEvent:Connect(function(player, currentCharacter, classObject)
	local ChosenCharacter = Characters[currentCharacter.Name]:Clone()
	local isHeroBool = ChosenCharacter.IsHero
	local guiClone
	
	if isHeroBool.Value == true then
		guiClone = heroMissionGui:Clone()
	else
		guiClone = villainMissionGui:Clone()
	end
	
	local ranBefore = false
	
	local function makeCharacter()
		local CurrentCharacter = player.Character
		local class = tostring(classObject.Value)
		local classScriptFolder = serverStorage.ScriptStorage["Class"..class]
		local LocalScripts = {}
		
		for index2,item2 in pairs(classScriptFolder:GetChildren()) do
			if item2:IsA("LocalScript") or item2:IsA("Script") then
				table.insert(LocalScripts,item2:Clone())
			else
				item2:Clone().Parent = ChosenCharacter
			end
		end
		
		CurrentCharacter.Health:Clone().Parent = ChosenCharacter
		table.insert(LocalScripts,CurrentCharacter.Animate:Clone())
		ChosenCharacter.Parent = workspace
		if ranBefore == false then 
			ranBefore = true
			ChosenCharacter.Name = player.Name
		end
		player.Character = ChosenCharacter
		
		for index2,item2 in pairs(LocalScripts) do
			item2.Parent = ChosenCharacter
		end
		
		guiClone.Parent = player.PlayerGui
		
		SetSubject:FireClient(player,ChosenCharacter.Humanoid)
		local Connection
	end
	
	makeCharacter()
	
	local function onDied()
		wait(game.Players.RespawnTime)
		player:LoadCharacter()
		wait(1)
		makeCharacter()
	end
	
	ChosenCharacter.Humanoid.Died:Connect(onDied)
end)

Why are you using makeCharacter() and LoadCharacter at the same time? Does makeCharacter do something LoadCharacter doesn’t?

makeCharacter() is the function that replaces the player’s character with the custom character, I need to have something to replace though, and also, it’s not at the same time because I added a wait(1) to it

It’s the function that I originally ran before the player died, it worked fine when I did that.

Just use a StarterCharacter it should load as the character every time the character resets.

No, I don’t want the same for all players though, I need it to Respawn the character depending on what the player chose

Hold on, the guy I’m working for just told me that it’s supposed to bring the menu back when the player dies anyway, sorry for wasting your time, but this thread may be pointless if I find a way to bring back the menu and do that, if I can I’ll update you.

Could this line have anything to do with it?

It’s the health script inside the player

This looks like what is causing it. Instead of just setting their character to it clone the necessary items into it. It is the only reason the character should be disappearing. It may also be because there are no parts to make up the character.

But why is it working the first time though?

Good point.

Then it maybe this. Put game.Players.CharacterAutoLoads = false in a ServerScript so this won’t be running over the original loading sequence

1 Like

Good update! It’s working now, thanks a ton for your help, you made my day!

1 Like