DataStore not working

So I have been working on this Datatore and when I finished it did not save my data and i was wondering if anyone knows why.

here’s the script:

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"

local Points = Instance.new("IntValue", leaderstats)
Points.Name = "Points"
Points.Value = 0

local playerUserId = "Player_"..player.UserId

-- load data
 
    
local success, errormessage = pcall(function()
    data = myDataStore:GetAsync(playerUserId)
end)

if success then
    Points.Value = data
    -- set our data to points
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId

local data = player.leaderstats.Points.Value

local success, errormessage = pcall(function()
    myDataStore:setAsync(playerUserId, data)
end)

if success then
    print("Data saved")
else
    print("There was a error")
    warn(errormessage)
end

end)

Thank you whoever helped

2 Likes

you wrote setAsync instead of SetAsync

1 Like

You might get yield on Player- you should use player-

Also re write the setAsync to SetAysnc

I suggest use DataStore2 … there a night mare using normal data store makes you lost a data of a players.

1 Like
local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"

local Points = Instance.new("IntValue", leaderstats)
Points.Name = "Points"
Points.Value = 0

local playerUserId = "Player_"..player.UserId

-- load data
 
    
local success, errormessage = pcall(function()
    return myDataStore:GetAsync(playerUserId)
end)

if success then
    Points.Value = errormessage -- ignore the name man
    -- set our data to points
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId

local data = player.leaderstats.Points.Value

local success, errormessage = pcall(function()
    myDataStore:SetAsync(playerUserId, data)
end)

if success then
    print("Data saved")
else
    print("There was a error")
    warn(errormessage)
end
end)
2 Likes