Problem with DataStoreService

Hey all, I’ve been having problems with the DataStore service. ApiServices are enabled. Below script is in ServerScriptService.

local DataStoreService = game:GetService("DataStoreService")
local Muffinz = DataStoreService:GetDataStore("abc123")
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Muffins = Instance.new("IntValue", leaderstats)
	Muffins.Name = "time wasted"
	
	local success, errormessage = pcall(function()
		 data = Muffinz:GetAsync("Player_".. tostring(player.UserId))
	end)
	if success then
		print(errormessage) -- returns nil
		print(data) -- also returns nil
		Muffins.Value = data
		print("Loaded data successfully")
	else
		print("New user:"..player.Name)
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local data = player.leaderstats["time wasted"].Value
	local success, errormessage = pcall(function()
		Muffinz:SetAsync("Player".. tostring(player.UserId), data)		
	end)	
	if success then
		print("DataSaved")
	else 
		print("ThereWasAnError")
		warn(errormessage)
	end
end)

ClickDetector that adds one point to value

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	player:WaitForChild("leaderstats")["time wasted"].Value = player:WaitForChild("leaderstats")["time wasted"].Value + 1 
end)

The top script doesn’t work, whether it be in or out of Studio.

1 Like

Any errors? Can you send output?

1 Like

I spotted the error.

You’re using two different keys in your getters and setters. In your setter, you’re missing an underscore.

On a different note, it’s actually pointless to use tostring, as Lua handles concatenation automatically for you.

5 Likes

Thank you so much, this worked.

1 Like