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

Can you guys help me find out what's wrong with my tp script?

Asked by 9 years ago

It saves the player's position when they leave then teleports them there when they rejoin, I honestly do not know what went wrong. It is probably with the datastore.

game.Players.PlayerAdded:connect(function(p)
    pos = Instance.new("CFrameValue",p)
    pos.Name = "position"
    p.CharacterAdded:connect(function(c)
        if game:GetService("DataStoreService"):GetDataStore("data1"):GetAsync(p.Name.."_pos") then
            c.Torso.CFrame = game:GetService("DataStoreService"):GetDataStore("data1"):GetAsync(p.Name.."_pos")
        end
    end)
end)
game.Players.PlayerRemoving:connect(function(p)
    game:GetService("DataStoreService"):GetDataStore("data1"):SetAsync(p.Name.."_pos",p.position.Value)
end)

while true do
    for i,v in pairs(game.Players:GetChildren()) do
        if v.Character:FindFirstChild("Torso") then
            v.position.Value = v.Character.Torso.CFrame
        end
    end
    wait()
end

1 answer

Log in to vote
0
Answered by 9 years ago

Some notes:

  • On line 6, you retrieve the value from the datastore regardless of if you already have a more up-to-date version (though this depends on if you want them to teleport to where they just were when they die, or if you want them to go back to where they started when they joined the server). If it's the former, you should say c.Torso.CFrame = pos.Value (but be sure to set pos.Value to a spot where they can spawn safely).
  • You while loop can break if v.Character is ever nil (ex character not loaded)
  • Datastores can only save some basic values, like tables, strings, and numbers. To my knowledge, you cannot store a CFrame directly (though I admit to not having tried. It should tell you when this fails in the Output; you should give us any such errors (or lack thereof)) You can make a CFrame with two positions: CFrame.new(position, positionToLookAt). Assuming I'm right about the CFrames not being stored in a datastore, I recommend storing these two Vector3's either in a table or as a string. Ex, you might store it like this "10,5.1,-3.2,11,5.1,-3.2", where the first 3 numbers are the XYZ of position and the last 3 numbers are the XYZ of the positionToLookAt
Ad

Answer this question