Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago

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
Ad

Answer this question