How to check for data

Hi Scripters of Roblox, how can I make it so that when a user joins a game, it checks for a Value in a data store, if it has a value then thats the value of the users leaderstat, but if there isnt any data in the data store, then the leaderstat = 0

4 Likes

If you want to get data, you need to use :GetAsync(). You can learn more about data stores here.

2 Likes

Yeah, im saying is it Money.Value = MoneyStore:GetAsync(player.Id) or 0?

1 Like

Because cant the value be nil if it hasnt been set

1 Like

This guy will teach you all about it!

1 Like

With this code, the value will be 0 if there is no data.

Money.Value = MoneyStore:GetAsync(player.UserId) or 0
2 Likes
local function OnPlayerAdded(Plr)
	local UserD;
		local Success,
			  Error = pcall(function()
				UserD = DS:GetAsync(queryplr.UserId);end);
		if Error then warn("Failed to load "..queryplr.Name.." Data") return; end; --< You can make repetitive calls if you want, I normally don't :/
if(not UserD)then UserD=CreateNewData(); end;
end;

Always try to wrap external calls in a pcall

1 Like

Huh. That is so advanced lmaooo. Ik what pcall is but

1 Like

Here you can check my tutorial if you want!

It has all sort of stuff you can learn for datastores.

Btw if the player doesn’t have any data the automatic will be set to 0.

1 Like

That will wipe their data if there’s an error retrieving it. I don’t think that’s what pcall should be used for.

2 Likes

Thank you for that, maybe I should stop using the devforum at 2 AM. I’ll edit my post also