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

How do I turn a wait() into a PlayerAdded script?

I want spawn #1 to be enabled when the character first loads in, and then when the player resets it will respawn in a different location, using spawn #2. My script works fine in Studio, but bugs out in Roblox. Script:

workspace.Model.SpawnLocation.Enabled = true --Spawn #1
workspace.Cell.SpawnLocation.Enabled = false --Spawn #2
wait(5)
workspace.Model.SpawnLocation.Enabled = false
workspace.Cell.SpawnLocation.Enabled = true

1 answer

✓ Accepted answer

Paste this in a Local Script, and put it in StarterPlayerScripts under the StarterPlayer folder or StarterPack.

-- Wait for the player's character to load in
repeat wait() until game.Players.LocalPlayer.Character
repeat wait() until game.Players.LocalPlayer.Character:IsDescendantOf(workspace)
----------------------------------------------------------------------------------------------------------------

--VARIABLES--
local Character = game.Players.LocalPlayer.Character

local Spawn1 = workspace.Model.SpawnLocation
local Spawn2 = workspace.Cell.SpawnLocation

--FUNCTIONS--
function ENABLE_SPAWN()
	if not Spawn1.Enabled and not Spawn2.Enabled then -- if both spawns are disabled, then enables Spawn1
		Spawn1.Enabled = true 
		Spawn2.Enabled = false 

	elseif not Spawn1.Enabled and Spawn2.Enabled then -- if Spawn2 is enabled and Spawn1 is disabled, enables Spawn1
		Spawn1.Enabled = true 
		Spawn2.Enabled =false 

	elseif not Spawn2.Enabled and Spawn1.Enabled then -- if Spawn1 is enabled and Spawn2 is disabled, enables Spawn2
		Spawn1.Enabled = false 
		Spawn2.Enabled = true 
	end
end

function ON_DEATH()
	ENABLE_SPAWN() -- calls function ENABLE_SPAWN()
end

Character.Humanoid.Died:connect(ON_DEATH) -- calls function ON_DEATH() when the player dies

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