Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

DataStore not working/saving values?

The error is 11:57:58.555 - ServerScriptService.Script:48: bad argument #1 (Lua function expected), on the coroutine. Here is the script:

local DataStoreService = game:GetService("DataStoreService")
local save = DataStoreService:GetDataStore("save")
local players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player)
	local DataValues = Instance.new("Folder")
	DataValues.Name = "DataValues"
	DataValues.Parent = player
	
	local KoalaCounter = Instance.new("IntValue")
	KoalaCounter.Name = "KoalaCounter"
	KoalaCounter.Parent = DataValues

	local Data2
	
	local success, err = pcall(function()
		
		Data2 = save:GetAsync(player.UserId.."-KoalaPoints", KoalaCounter.Value)
		
	end)
	if success then
		
		KoalaCounter.Value = Data2
		
		print("Success")
		
	else
			print("Error getting data")
			warn(err)
	end
	
	end)
game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		save:SetAsync(player.UserId.."-KoalaPoints", player.DataValues.KoalaCounter.Value)
		
	end)
	
	if success then
		print ("data saved")
	else
			print("There was an error")
			warn(err)
	end
end)
game:BindToClose(function()
    for _, player in ipairs(players:GetPlayers()) do
        coroutine.wrap(save)(player)
    end
end)
	

1 answer

"save" isn't a function, it's the variable representing your DataStore. I'd suggest you make a function that saves your data, such as

function saveData(player)
	save:SetAsync(player.UserId.."-KoalaPoints", player.DataValues.KoalaCounter.Value)
end

and change line 48 to

coroutine.wrap(saveData)(player)

This discussion is preserved as it was. New answers, comments, and votes are closed.