The output doesn't print any error, it's just that it never saves or loads the data because it always prints "New Player". I have the Studio Access to API Services enabled and i did publish the game so i can't see what's wrong.

--//Services
local dataStore = game:GetService('DataStoreService'):GetDataStore('DataStoreName') 

--//CurencyName
local currencyName = 'Gold'

--//PlayerAdded
game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new('Folder')
	folder.Name = 'leaderstats' 
	folder.Parent = player
	
	local currency = Instance.new('IntValue')
	currency.Name = currencyName 
	currency.Parent = folder
	
	local ID = currencyName..'-'..player.UserId 
	local savedData = nil 
	
	pcall(function()
		local savedData = dataStore:GetAsync(ID)
	end)
	
	--//If player has data then load it
	if savedData ~= nil then
		currency.Value = savedData
		print('Data Loaded')
	else
		--//Else if player doesn't have data then give them 500 Gold
		currency.Value = 500
		print('New Player')
	end
end)

4 comments

0

why do you need to wrap :GetAsync() in a pcall

0

Well according to a video that i saw, it is used to prevent the script from breaking entirely if an error occurs.

0

pcall will return the error for you. first it returns false, and then a message with what the error is. take a look here >> https://developer.roblox.com/articles/Built-in-Functions-and-Variables/Lua#pcall

0

I removed the pcall function and it worked, i'm still not sure why the person in the video i watched used the pcall function, it worked for him though.