When a new player joins a value should one but is zero

This Is The Script

local datasevice = game:GetService("DataStoreService")

local mydatastore = datasevice:GetDataStore('MyDataStore')

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local coins = Instance.new("NumberValue",leaderstats)
	coins.Name = "Coins"
	coins.Value = 100000
	
	local multiplier = Instance.new("NumberValue",leaderstats)
	multiplier.Name = "Multiplier"
	multiplier.Value = 1
	
	local dataC
	local dataM
	local success, errormessage = pcall(function()
		dataC = mydatastore:GetAsync(player.UserId.."-Coins")
		dataM = mydatastore:GetAsync(player.UserId.."-Multiplier")
	end)
	
	if success then
		coins.Value = dataC
		multiplier.Value = dataM
	else
		player.leaderstats.Multiplier.Value = 1
		print("ERROR")
		print(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		mydatastore:SetAsync(player.UserId.."-Coins",player.leaderstats.Coins.Value)
		mydatastore:SetAsync(player.UserId.."-Multiplier",player.leaderstats.Multiplier.Value)
	end)
	
	if success then
		print("Data Success")
	else
		print("ERROR")
		warn(errormessage)
	end
end)

GetAsync doesn’t error if the key doesn’t exist. It succeeds and returns nil.

So check the return value, not just success

I suggest learning how to use Datastore2, because it’s a lot easier than going through the trouble of making your own data storing system. I really don’t see any problems in your code.

thanks learned a lot from this video

1 Like