Datastore not saving

local ReplicatedStorage = game.ReplicatedStorage

local DataStoreService = game:GetService("DataStoreService")

local FirstPlayStore = DataStoreService:GetDataStore("FirstPlayStore")

local function SaveData(Player)

local success, errormessage = pcall(function()

FirstPlayStore:SetAsync(Player.UserId.."-FirstPlay",Player.PlayerStats.FirstPlay.Value)

end)

if success then

print(Player.PlayerStats.FirstPlay.Value)

warn("Saved data")

else

warn(errormessage)

end

end

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

local PlayerStats = Instance.new("Folder",Player)

PlayerStats.Name = "PlayerStats"

local FirstPlay = Instance.new("IntValue",PlayerStats)

FirstPlay.Name = "FirstPlay"

FirstPlay.Value = 1

SaveData(Player)

end)

game.Players.PlayerRemoving:Connect(function(Player)

SaveData(Player)

end)

game:BindToClose(function()

for i, Player in pairs(game.Players:GetChildren()) do

SaveData(Player)

end

end)

Its says “saved data” but when i am rejoin the firstplay value returns 1

I did it thanks…

1 Like

You didn’t use GetAsync()

local ReplicatedStorage = game.ReplicatedStorage

local DataStoreService = game:GetService("DataStoreService")

local FirstPlayStore = DataStoreService:GetDataStore("FirstPlayStore")

local function SaveData(Player)
local success, errormessage = pcall(function()
	FirstPlayStore:SetAsync(Player.UserId.."-FirstPlay",Player.PlayerStats.FirstPlay.Value)
end)
if success then
	print(Player.PlayerStats.FirstPlay.Value)
	warn("Saved data")
else
	warn(errormessage)
end
end

local function GetData(Player,FirstPlay)
local UserId = Player.UserId.."-FirstPlay"
local Data = FirstPlayStore:GetAsync(UserId)
print(Data)
if Data then
	FirstPlay.Value = Data
else
	FirstPlay.Value = 1
end
end

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

local PlayerStats = Instance.new("Folder",Player)
PlayerStats.Name = "PlayerStats"

local FirstPlay = Instance.new("IntValue",PlayerStats)
FirstPlay.Name = "FirstPlay"

FirstPlay.Value = 1
GetData(Player,FirstPlay)
end)

game.Players.PlayerRemoving:Connect(function(Player)
SaveData(Player)
end)

game:BindToClose(function()
for i, Player in pairs(game.Players:GetChildren()) do
	SaveData(Player)
end
end)
2 Likes