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

Respawning where I left last time?

Asked by 4 years ago

My title explains everything I want to do. However I have no idea how to do it. I thought of a spawnlocation but that would be very messy. So I guess the only way is datasave, how do to it correctly though? Please help!

P.S. I am not asking for a script! I just want someone to show me how I should do to accomplish my objective.

0
Save the location when they leave in a datastore, then load it when they join and teleport them to that point. MrLonely1221 701 — 4y

1 answer

Log in to vote
0
Answered by
Alphexus 498 Moderation Voter
4 years ago
Edited 4 years ago

To make this, we need to save the position in a DataStore. Here is a rough example.

local PositionStore = game:GetService("DataStoreService"):GetDataStore("PositionStore")
game:GetService("Players").PlayerAdded:Connect(function(player)
    local Character = player.Character or player.CharacterAdded:Wait()
    local Success, LastPosition = pcall(function()
        return PositionStore:GetAsync(player.UserId)
    end)
    if Success and LastPosition ~= nil then
        wait(1)
        Character:MoveTo(Vector3.new(LastPosition.X, LastPosition.Y, LastPosition.Z)
    end
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
    local Character = player.Character
    pcall(function()
        local CurrentPosition = {
            X = Character.HumanoidRootPart.Position.X,
            Y = Character.HumanoidRootPart.Position.Y,
            Z = Character.HumanoidRootPart.Position.Z
        }
        PositionStore:SetAsync(player.UserId, CurrentPosition)
    end)
end)
0
Yes, I've tried doing something like that by creating a my own code and it didn't work because of some errors. I have just tried yours and it gives no errors, but everytime I join my game I keep spawning in the place I usually spawn. (Yes I moved before leaving...) What's wrong? Could you help me further? Fixer1987 95 — 4y
0
Fixer1987 Most data does NOT save in studio try it in an actual game EnzoTDZ_YT 275 — 4y
0
@EnzoTDZ_YT I did that! Doesn't work... Fixer1987 95 — 4y
0
This code should work. I don't see any problem with it. Are you sure its a server(regular) script and not a local script? I would put this in ServerScriptService in a regular script. Alphexus 498 — 4y
View all comments (10 more)
0
@Alphexus It isn't. I put it in ServerScriptService as a script and it doesn't work. Everytime I leave the game I published I keep respawning... Fixer1987 95 — 4y
0
Try waiting like 3 seconds before moving the character. Alphexus 498 — 4y
0
Still not working. :/ Fixer1987 95 — 4y
0
I see why its not working. I did not know that you cannot save Vectors in a DataStore. I will get back to you with an answer. Alphexus 498 — 4y
0
Post is edited and updated. Try out the new code. I saved the components of the position in a table and then loaded it. This should work. Please accept the answer if it works. Thanks! Alphexus 498 — 4y
0
wait wait wait... ScriptingHelpers is a site were you Help with already existing script. Not give the asker a script. and your Reputation as above 300... Luka_Gaming07 534 — 4y
0
So? Some people learn better with code examples. I chose to answer it this way even though the person clearly stated they did not want a script. I just find it easier to give a code example and a very brief explanation. Alphexus 498 — 4y
0
Tahnk you so much! Trying it right now. Will let you know. Fixer1987 95 — 4y
0
Did it work? Alphexus 498 — 4y
0
you literally just gave away a script to someone who didnt even start making one Gameplayer365247v2 1055 — 4y
Ad

Answer this question