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

My position script does not save position and does not print?

Asked by 4 years ago

Hi, I'm making an obby and I'm trying to make a script that saves the position when a player leaves and puts them in the same position when they rejoin. It just keeps on bringing me back to the original SpawnLocation. Please help!

Code:

DataStoreService = game:GetService("DataStoreService")
positionStore = DataStoreService:GetDataStore("positionStore")

game.Players.ChildAdded:Connect(function(plr)
    local success, position = pcall(function()
        return positionStore:GetAsync(plr)
    end)
    if not position then
        local success2, err2 = pcall(function()
            positionStore:SetAsync(plr, tostring("-2, 1.5, 54"))
        end)
        if success2 then
            print("Successfully saved position for " .. plr.Name .. "!")
        elseif err2 then
            error("Error saving position for " .. plr.Name .. ". Error: " ..  err2)
        end
    else
        local character = plr.CharacterAdded:Wait()
        character:MoveTo(Vector3.new(tonumber(position)))
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local success3, err3 = pcall(function()
        positionStore:SetAsync(plr, tostring(plr.Character.Position))
    end)
    if success3 then
        print("Successfully saved data for " .. plr.Name .. "!")
    elseif err3 then
        error("Error saving data for " .. plr.Name .. ". Error: " ..  err3)
    end
end)
0
Im not sure if this would help your case, but if you could substitiute the SpawnLocation with a normal Part and just use the PlayerAdded function, and when the player touches the part (checkpoint), then you would save the players' position. Hope this helps! TheEggsquidzidBoi 38 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Nevermind, I will switch to teams.

Ad

Answer this question