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

How to check if lives reach Zero and then reset player to Starter Spawn?

I have gotten everything working so far just trying to get the player to reset to starter spawn once lives reach zero then reset player lives and stage number. I seem to be caught up on the teleporting back to spawn to begin with. Also this script is in the Workspace and I was wondering if this is only working for the player or for all players who will join.

game.Players.PlayerAdded:Connect(function(plr)
    local Stats = Instance.new('Folder') 
    Stats.Name = 'leaderstats'
    Stats.Parent = plr
 
    local Lives = Instance.new('IntValue')
    Lives.Name = 'Lives'
    Lives.Value = 3
    Lives.Parent = Stats
 
    local Stage = Instance.new('IntValue')
    Stage.Name = 'Stage'
    Stage.Value = 1
    Stage.Parent = Stats
 
    local Timer = Instance.new('IntValue')
    Timer.Name = 'Time'
    Timer.Value = 0
    Timer.Parent = Stats
 
    plr.CharacterAdded:Connect(function(char)
        char.Humanoid.Died:Connect(function()
            Lives.Value = Lives.Value -1
        end)
    end)
 
    while true do
        wait(1)
        Timer.Value = Timer.Value +1
    end
end)

if Lives.Value == 0 then tpToStart()
	
end

2 answers

✓ Accepted answer

Not home, again, to see if this is working correctly. I left some comments down in the code.

game.Players.PlayerAdded:Connect(function(plr)
    local Stats = Instance.new('Folder') 
    Stats.Name = 'leaderstats'
    Stats.Parent = plr

    local Lives = Instance.new('IntValue')
    Lives.Name = 'Lives'
    Lives.Value = 3
    Lives.Parent = Stats

    local Stage = Instance.new('IntValue')
    Stage.Name = 'Stage'
    Stage.Value = 1
    Stage.Parent = Stats

    local Timer = Instance.new('IntValue')
    Timer.Name = 'Time'
    Timer.Value = 0
    Timer.Parent = Stats

    plr.CharacterAdded:Connect(function(char)
        char.Humanoid.Died:Connect(function()
            Lives.Value = Lives.Value -1
            if Lives.Value <= 0 then --if the lives is lower than or equal to
              	--keep in mind, every character should have a PrimaryPart. Now, we set that PrimaryPart's CFrame to (workspace.SpawnPart.CFrame)
				Stage.Value = 0
				Lives.Value = 3
              	char:SetPrimaryPartCFrame(workspace.SpawnPart.CFrame) --Where it says 'SpawnPart', just change the name.
            end
        end)
    end)

    while true do
        wait(1)
        Timer.Value = Timer.Value +1
    end
end)

If this worked, if you don't mind, accept my answer, thanks!

~FiredDusk (Aka: Previized)

5 comments

0

FiredDusk thank you once again the question I ask is would I change the SpawnPart to the name of the spawn?

0

Yes you would. Make sure that part would be directly in workspace.

0

Thank you I got it working now I need to change the Stage Value and the Lives value back to default values once I hit zero. I see that the data is under the player data while the game is running under leaderstats so would it be Players.LocalPlayer.leaderstats.Lives?

0

No, never get the "LocalPlayer" in a server script, it's only used in local scripts. Also, I will edit the script to change the values back.

0

I edited it.

Hm, now there are multiple ways you could go about doing this but I have thought of two main ways of doing it. Now it seems like you know what you are doing so I will give you some things to go off of and research instead of just giving you the direct answer.

Let's start out with figuring out when lives = 0 I will give you this script since it's pretty simple.

	--Make this simple while wait do loop, you can add your other if statements to this
while wait() do
	if Lives.Value = 0 then
		--Here you can add what should happen when you lose all 3 lives
	end
end

Method #1 This method that I thought of uses the old teams system in Roblox. You might remember it if you're an old school player however it's not used much anymore. This would allow you to set one spawn as, say, blue team spawn and another as red team spawn. Blue team could be dead 0 lives and red could be still alive.

Method #2 Now this one will require a bit more work but it's much better and knowing it know will definitely help you out down the road. Instead of setting your spawn simply just teleport the player. Now I don't mean to make this sound like it's difficult, it's fairly easy actually! There is a lot you can learn about it but for you want just go check out this link here.

4 comments

0

I am using a checkpoint system instead of the old team system. I was trying to teleport to the original spawn before but it wasn't working. either I had the code wrong from the teleport wiki page you posted or I didn't have the if action correct.

0

so this is what I changed it to and I am not getting any result

0

while wait() do if Lives.Value == 0 then game.Workspace.Player.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-1, 614.6, -2645)) end end

0

Is this persons code working for you @Social?

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