Hi,

I've recently started using DataStoreService for the first time, and wanted to implement them into my game to store users' Credits. It doesn't seem to error anywhere else, but it fails to get the player's data when they join the game.

Here's my code:

local DSS = game:GetService("DataStoreService")
local playerCreditScore = DSS:GetDataStore("playerCreditScore")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player
	local Credits = Instance.new("IntValue")
	Credits.Name = "Credits"
	Credits.Parent = leaderstats
	
	local function getCredit()
		return playerCreditScore:GetAsync(Player.UserID)
	end
	
	local success, credit = pcall(getCredit)
	
	if success then
		Credits.Value = credit
	else
		warn("Could not get ", Player.Name,"'s credits.")
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local success = pcall(function()
		playerCreditScore:SetAsync(Player.UserId, Player.leaderstats.Credits.Value)
	end)
	if success then
		print("Successfully saved ", Player.Name,"'s credits.")
	else
		warn("Could not save ", Player.Name,"'s credits.")
	end
end)

FYI, the game has Filtering Enabled enabled.

Thanks

6 comments

0

Why did you do :IncrementAsync(). Does that work if the values were never there in the first place? How's it going to increment numbers that were never SetAsynced before-hand? Just wondering because I never use :IncrementAsync() and haven't seen it use in a while or ever I guess.

0

hmm, I just figured it was relevant because money is numerical. I'll try SetAsync.

0

--it didn't change anything.

0

In line 13. UserId, not UserID

0

You should have made that an answer, lol

0

--or not, it didnt work.