How can I make my character respawn where I die? (for anyone playing)

Hi! My name is Twisted, I’m making a little introduction about myself since this is my first topic post ever! I’m really excited to ask you guys questions and tell you everything I’ve tried and have done. A few welcomes would be very appreciated! :two_hearts: Anyway… let’s get to the point.

I’m currently making a running game, but I’m not sure how to make my character respawn where I died. I’m new to scripting so I don’t know too much about things that are simple but I’m learning and getting better each day. So a bit of help where I could find where to do it or a script so I can visualize how to do it would be very helpful!

I haven’t attempted to make this since I couldn’t find a source to learn/see how to do it.

5 Likes

No, you didn’t read it correctly. I want my character to respawn where I die. I don’t want to die and respawn back to the start.

For example:
If I’m running away from the spawn point and I hit something that kills me. Then when I respawn, I respawn where I died.

Oops i misread, i thought you said when.

You want to save the position they died at, with humanoid.Died, and then wait 5 seconds for them to respawn

character.Humanoid.Died:Connect(function()
    local last_cf = character:GetPrimaryPartCFrame()
    wait(5)
    character:SetPrimaryPartCFrame(last_cf)
end)
1 Like

It’s underlining in orange under every “character” word. Is there a solution to this?

That was meant to ben pseudocode, not a fully working script. you seem to be new to scripting I encourage you learn basics like variables and the data types of Lua before making a project.

I said that, but thanks for the info!

edit: Do you know any sources that helped you learn variables and data types? I have some, but if you have any suggestions I would like to know them.

Here’s how I would do it,

game.Players.PlayerAdded:Connect(function(Player)
-- This function will run whenever someone new joins the game.

Player.CharacterAdded:Connect(function(Character)
--This function will run whenever that player's character spawns.

Character.Humanoid.Died:Connect(function()
-- This function will run whenever that player's character's humanoid dies

local PositionWhenDied = Character.Torso.Position
Player.CharacterAdded:Wait() -- Wait for the new character to spawn.
Player.Character:MoveTo(PositionWhenDied) --Move the character to the old position

end)
end)
end)

You can copy and paste this into a script in workspace or ServerScriptService or wherever.

4 Likes

Put this as a Script in ServerScriptService:

game.Players.PlayerAdded:Connect(function(player)
    local spawn_point
    player.CharacterAdded:Connect(function(char)
        if spawn_point then
            char:MoveTo(spawn_point)
        end
         char:WaitForChild("Humanoid").Died:Connect(function()
              spawn_point = char.HumanoidRootPart.Position
          end)
    end)
end)
2 Likes

You should watch alvinblox beginner scripting tutorials, they’re great.

It didn’t work, I’m not sure what the problem is but I did realize the script goes away when I test the game. Does that happen naturally in Roblox Studio?

I have, and the tutorials are great! But, the good tutorial episodes weren’t coming out often so I stopped watching Alvin. Thanks for telling me though! ^^

If you put it in ServerScriptService, that’d make sense, as that service is only accessible for the server, and by default in Studio test mode you’re in client-mode.

Do you get any errors? I really think I wrote the correct logic :frowning:

I’m not sure exactly about the code, but I think if it’s like a running game, and there’s possibly levels? I think if you created a spawn, and then added it to a team, so let’s say I died in “The Desert”, I would respawn where the spawn for “The Desert” is because I had a check point there. I’m not sure about the coding for it, so you should watch AlvinBlox’s coding channel and watch videos of where he is scripting spawns for different teams. Sorry if I wasn’t of much help.

1 Like

Well, maybe try searching up tutorials for different concepts on YouTube to learn more about them, like events, functions, while loops, for loops, and if/then statements, that’s how I started. Also, try out my suggestion up above, that should work. :slight_smile:

I saw an AlvinBlox video about what you just explained but that’s not what I’m going for. I need my character to spawn exactly where they die. Thanks for the help though!

@C_Sharper The script goes away whenever I test the script.

@C0lvy123 Sadly, the script didn’t work that you gave me.

1 Like

Ah, so like if I died in the water, you’d want me to spawn exactly where I died. Hm, this is a bit tricky, I’m not sure if any other developer has ever done this before, but I think to do this, you’d have to be an advanced scripter. I suggest maybe learning from someone who knows Lua or Java as they’re both easy to learn. I might be able to help later on, but for now I think you may have to take some classes if you can.

Edit: Do you mean when they die it’s like a checkpoint? Sorry I’m confused, if they were to respawn where they died, they would just die for the whole duration of their time spent in-game.

Oh, you have to go to drafts and click commit next to the script. Then playtest and my script should work. You can find drafts in the view tab, then right click the script you want and “commit” should pop up.

1 Like

That’s only for Team Create; which I doubt he’s using.

Still useful information though.

1 Like

I mean he probably is, both our scripts aren’t working.

1 Like

I’m a solo developer, I have no one to work with currently. So no, I’m not using Team Create.