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

Instance new datastore script not creating an instance?

Asked by 6 years ago

I tried to do an instance new CFrame value to store the players position, but it didn't worked out, weird because I can save another value, when I see in the playerstats, there is no position value

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStore = game:GetService("DataStoreService")
local PlayerSave1 = DataStore:GetDataStore("PlayerSave1")
local PositionSave = DataStore:GetDataStore("PositionSave")
local Save = ReplicatedStorage:WaitForChild("Save")

local spawnPlace = workspace.SpawnPos

game.Players.PlayerAdded:connect(function(player)
    local PlayerStats = Instance.new("Folder", player)
    PlayerStats.Name = "PlayerStats"

    local HasJoinedBefore = Instance.new("BoolValue", PlayerStats)
    HasJoinedBefore.Name = "HasJoinedBefore"
    HasJoinedBefore.Value = PlayerSave1:GetAsync(player.UserId) or false

    PlayerSave1:SetAsync(player.UserId, HasJoinedBefore.Value)

    local Pos = Instance.new("CFrameValue", PlayerStats)
    Pos.Name = "Position"
    Pos.Value = PositionSave:GetAsync(player.UserId) or spawnPlace.CFrame

    player.Character.HumanoidRootPart.CFrame = Pos.Value

    PositionSave:SetAsync(player.UserId, Pos.Value)

    HasJoinedBefore.Changed:connect(function()
        PlayerSave1:SetAsync(player.UserId, HasJoinedBefore.Value)
    end)
end)

game.Players.PlayerRemoving:connect(function(player)
 PlayerSave1:SetAsync(player.UserId, player.PlayerStats.HasJoinedBefore.Value)
end)


local function save(player)
    player.PlayerStats.HasJoinedBefore.Value = true
    PlayerSave1:SetAsync(player.UserId, player.PlayerStats.HasJoinedBefore.Value)
end

Save.OnServerEvent:Connect(save)

Answer this question