The title says it all. It’s an autosave script, and the while true is not executing.
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("playerData")
local function saveData(player) -- The functions that saves data
local tableToSave = {
player.leaderstats.Cash.Value, -- First value from the table
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!")
warn(err)
end
end
local autoSave = true
while 1+1 == 2 do
saveData(game.Players.LocalPlayer)
print("If you can see this that means that IT WORKS!!!! Hopefully...")
wait(0.5)
end
game.Players.PlayerAdded:Connect(function(player)
local data = dataStore:GetAsync(player.UserId)
player.leaderstats.Cash.Value = data[0]
end)
game.Players.LocalPlayer can only be used on client. Try this:
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("playerData")
local function saveData(player) -- The functions that saves data
local tableToSave = {
player.leaderstats.Cash.Value, -- First value from the table
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!")
warn(err)
end
end
local autoSave = true
game.Players.PlayerAdded:Connect(function(player)
while wait() do
saveData(player)
print("If you can see this that means that IT WORKS!!!! Hopefully...")
wait(0.5)
end
local data = dataStore:GetAsync(player.UserId)
player.leaderstats.Cash.Value = data[0]
end)
I do not recommend setting it to 0.5 seconds as it can cause yelds in the Datastore
18:41:54.702 Data hasn’t been saved! - Server - Script:17
18:41:54.702 HTTP 401 (Unauthorized) - Server - Script:18
18:41:54.703 If you can see this that means that IT WORKS!!! Hopefully… - Server - Script:30
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local dataStore = DataStoreService:GetDataStore("Data")
game.Players.PlayerRemoving:Connect(function(player)
game:BindToClose(function()
if dataStore:SetAsync(player.UserId) then
local success, result = pcall(function()
dataStore:SetAsync(player.UserId, data)
end)
if not success then
warn(result)
end
end
end)
end)
Try using bindtoclose instead of auto saving, it may benefit you