:LoadCharacter stopping rest of script

I have a client script and a server script that work together through a RemotEvent! When a player dies, it will pop up a GUI that takes up all the screen and disable that player’s controls, then it will prompt the player to disconnect and rejoin again. The problem I’ve found is that when the player dies, they autoload, and I’ve tried disabling this, but when the player first joins, you need to have a loaded character so they can play. My scripts were working perfectly, until I added a :LoadPlayer right after the PlayerAdded event.

Server Script

game:GetService('Players').PlayerAdded:Connect(function(player)
player:LoadCharacter() -- this line ruins everything, take this line out and everything works great
player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			print(player.Name .. " has died!")
			game:GetService("ReplicatedStorage").PlayerDied:FireClient(player)
		end)
	end)
end)

Client-Sided script

local client = game.Players.LocalPlayer

local playerModule = require(client.PlayerScripts:WaitForChild("PlayerModule"))

local controls = playerModule:GetControls()

game:GetService("ReplicatedStorage").PlayerDied.OnClientEvent:Connect(function(player)
	print("Got call from server")
	game:GetService("SoundService"):PlayLocalSound(workspace.FallenDownMusicAudio)
	script.Parent.ScreenGui.TextLabel.Visible=true
	client.Character.Humanoid.JumpPower=0
	controls:Disable()
	print("Disabled controls.")
end)
1 Like

You dont even need that line… You have a CharacterEvent event so forcing his character to load isn’t needed you can use Player.CharacterAdded:Wait() instead, or just remove that line and it will work perfectly fine

So the character will load either way? When I remove the :LoadCharacter and turn autoload off, it breaks the game, as it should because there is no character.

No it shouldn’t??? I never use loadCharacter and everything works perfectly fine

but CharacterAutoLoad is off, thats the problem!

1 Like

Why tho??? [Nvm this its for Characters limit]

Look at the order of your code, you load the character then connect the function to character added. If you load the character after the connection it will trigger. I believe your code will still work if you load the character again elsewhere or by respawning.

game:GetService('Players').PlayerAdded:Connect(function(player) -- 1. connect player added
    player.CharacterAdded:Connect(function(character) -- 2. connect character added, the function is not run until character loads
		character:WaitForChild("Humanoid").Died:Connect(function() -- 4. running the function; connect humanoid died
			print(player.Name .. " has died!")
			game:GetService("ReplicatedStorage").PlayerDied:FireClient(player)
		end)
	end)

    player:LoadCharacter() -- 3. add character
end)

Also you can do your entire script on the client-side with this LocalScript in StarterCharacterScripts. Unless you really need the server to do something that you haven’t listed; you should prefer checking on the client.

local character = script.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
local player = game:GetService("Players").LocalPlayer
local playerModule = require(player.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playerModule:GetControls()

humanoid.Died:Connect(function()
	print("My character died locally")
	game:GetService("SoundService"):PlayLocalSound(workspace.FallenDownMusicAudio)
	player.PlayerGui.ScreenGui.TextLabel.Visible=true
	character.Humanoid.JumpPower=0
	controls:Disable()
	print("Disabled controls.")
end)

So when the player dies, they don’t respawn lol, I’m making a one-chance obby

1 Like

In studio, you can select the players service and disable character autoloading, and use the script that @gertkeno posted