Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how to stop the character from spawning until i press play?

when i die a button shows up i press it and the character will respawn

1 answer

Hello there.

Before explaining, make sure you've added a RemoteEvent within ReplicatedStorage (you can name it whatever you want)

So when the player dies, it will fire the .Died event. We can listen for this event and then hook up a function to it.

Now, make a function for when the player clicks the respawn button (assuming the button is a ScreenGui with a TextButton inside it with its .Visible property set to true). Create a LocalScript within StarterCharacterScripts and paste the following code in.

local Players = game:GetService('Players')
local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild('Humanoid')
local RemoteEvent = game:GetService('ReplicatedStorage'):WaitForChild('name of RemoteEvent')
local button = "location of button"

local function buttonClickedTrigger()
	RemoteEvent:FireServer()
	button.Enabled = false
end

button.MouseButton1Click:Connect(buttonClickedTrigger)

Following this, you can then hook up a simple function that sets Players.CharacterAutoLoads to false, before displaying the respawn button on the player's screen. (The code below will go in the same script as the code shown above)

local function playerDiedTrigger()
	if Players.CharacterAutoLoads then
		Players.CharacterAutoLoads = false
	end

	button.Enabled = true
end

Now, we simply have to hook up this function to the .Died event that I mentioned earlier.

humanoid.Died:Connect(playerDiedTrigger)

Oh, hold your horses there! We're not finished quite yet :) Since we've now set .CharacterAutoLoads to false, the game will no longer automatically respawn the character, meaning that we will manually have to do it ourselves.

Create a Script within ServerScriptService and paste the following code below. What this does is basically listen for the client's signal; Once it has received the signal, it will then proceed to load the character, as only the server can load the character.

local RemoteEvent = game:GetService('ReplicatedStorage'):WaitForChild('name of RemoteEvent')

local function loadCharacter(plr)
	plr:LoadCharacter()
end

RemoteEvent.OnServerEvent:Connect(loadCharacter)

If all goes well, then it should work just fine! If not, please tell me what the error is.

This discussion is preserved as it was. New answers, comments, and votes are closed.