The title is self-explanatory. Why does my data not save or load?
Here is my ServerScript, inside of ServerScriptService:
local dataStoreService = game:GetService("DataStoreService")
local datastore = dataStoreService:GetDataStore("File1")
local pointStat
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
pointStat = leaderstats:WaitForChild("Points")
if player then
print("yippe")
local data
local success, response = pcall(function()
data = datastore:GetAsync("file1")
end)
if success then
pointStat.Value = data
else
pointStat.Value = 0
warn("Error.")
end
end
end)
game.Players.PlayerRemoving:Connect(function()
local success, response = pcall(function()
datastore:SetAsync("file1", pointStat.Value)
end)
if success then
print("data saved")
else
warn("Error.")
end
end)
game:BindToClose:Connect(function()
for i, player in pairs(game.Players:GetPlayers()) do
local success, response = pcall(function()
datastore:SetAsync(player.UserId, pointStat.Value)
end)
if success then
print("data saved")
else
warn("Error.")
end
end
end)
also please use the player.UserId for your data keys
local dataStoreService = game:GetService("DataStoreService")
local datastore = dataStoreService:GetDataStore("File1")
local pointStat
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
pointStat = leaderstats:WaitForChild("Points")
if player then
print("yippe")
local data
local success, response = pcall(function()
data = datastore:GetAsync(player.UserId)
end)
if success and data then
pointStat.Value = data[1]
else
pointStat.Value = 0
warn("Error.")
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, response = pcall(function()
datastore:SetAsync(player.UserId, pointStat.Value)
end)
if success then
print("data saved")
else
warn("Error.")
end
end)
game:BindToClose:Connect(function()
for i, player in pairs(game.Players:GetPlayers()) do
local success, response = pcall(function()
datastore:SetAsync(player.UserId, pointStat.Value)
end)
if success then
print("data saved")
else
warn("Error.")
end
end
end)
also one question where r u getting the leaderstats and pointstat from?
local dataStoreService = game:GetService("DataStoreService")
local datastore = dataStoreService:GetDataStore("File1")
local pointStat
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = player:WaitForChild("leaderstats")
pointStat = leaderstats:WaitForChild("Points")
if player then
print("yippe")
local data
local success, response = pcall(function()
data = datastore:GetAsync(plr.UserId)
end)
if success then
pointStat.Value = data
else
pointStat.Value = 0
warn("Error.")
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, response = pcall(function()
datastore:SetAsync(plr.UserId, plr.leaderstats.pointStat.Value)
end)
if success then
print("data saved")
else
warn("Error.")
end
end)