While true script not working and other issues

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)
1 Like

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

It’s in a local script already

Datastore can not be used in a local script. Change it to server

1 Like

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

those are the errors. What is wrong?s

Make sure to enable API Services?

18:43:33.793 Unboxing Simulator publish failed. - Studio

I think my studio is broken

You can’t use data stores on the client.

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

wrote on mobile if errors pls no judge ;-;

401 means that you cant access the method that you want to access. Move the datastores to the server and try again.

Datastores are on server already

18:50:05.382 Failed to load sound rbxassetid://2157362489: Unable to download sound data - Studio
18:50:05.382 Failed to load sound rbxassetid://145486970: Unable to download sound data - Studio
18:50:08.712 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648451 because ‘could not fetch’ - Studio
18:50:08.797 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648342 because ‘could not fetch’ - Studio
18:50:08.797 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648407 because ‘could not fetch’ - Studio
18:50:08.797 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648469 because ‘could not fetch’ - Studio
18:50:08.797 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648337 because ‘could not fetch’ - Studio
18:50:08.797 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648303 because ‘could not fetch’ - Studio
18:50:08.805 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648264 because ‘could not fetch’ - Studio
18:50:08.827 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648395 because ‘could not fetch’ - Studio
18:50:08.827 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648380 because ‘could not fetch’ - Studio
18:50:08.829 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648319 because ‘could not fetch’ - Studio
18:50:08.829 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648364 because ‘could not fetch’ - Studio
18:50:08.829 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648441 because ‘could not fetch’ - Studio
18:50:08.829 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648424 because ‘could not fetch’ - Studio
18:50:08.829 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=1660648351 because ‘could not fetch’ - Studio
18:50:08.944 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=4248979925 because ‘could not fetch’ - Studio
18:50:08.946 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=4432796068 because ‘could not fetch’ - Studio
18:50:08.946 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=4072431046 because ‘could not fetch’ - Studio
18:50:08.946 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=167549828 because ‘could not fetch’ - Studio
18:50:08.970 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=4735308607 because ‘could not fetch’ - Studio
18:50:08.977 MeshContentProvider failed to process https://assetdelivery.roblox.com/v1/asset?id=4315739043 because ‘could not fetch’ - Studio
18:50:09.065 Localization Tools failed to get all languages info. - Studio
18:50:09.144 Unable to load plugin icon. - Studio
18:50:36.081 File is in use or is not writable. Opened for read-only. - Studio
18:50:37.214 Failed to load installed plugins from cloud: HttpRequest “UserDataStoreService::loadUserDataAsync()”: Got network error status: 401 - Studio
18:50:37.264 Failed to load beta features from cloud: HttpRequest “UserDataStoreService::loadUserDataAsync()”: Got network error status: 401 - Studio

What is happening!! This started all of a sudden

Maybe try saving a copy of the game and re-logging in

I have tried doing that already

This is prob because you haven’t published the game just yet, try also enabling HTTP Requests in the process

I’ve published it already its private