ゲーム内通貨システムを作るscript例【Roblox Studio】

方法

ServerScriptServiceの下
取得する例

local currencyName = "Coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
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()
		savedData = DataStore:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		currency.Value = savedData
		print("Data loaded")
	else
		-- New player
		currency.Value = 50
		print("New player to the game")
	end
	
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	
	-- When game is ready to shutdown
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end
	
	wait(5)	
	
end)

通貨を追加

local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")

local currencyName = "Coins"

local PreviousPurchases = DataStoreService:GetDataStore("PreviousPurchases")

local ONE_HUNDRED_CASH = 344541178

MarketplaceService.ProcessReceipt = function(receipt)
	
	-- Receipt has PurchaseId, PlayerId, ProductId, CurrencySpentValue, CurrencyType, PlaceIdWherePurchased
	
	local ID = receipt.PlayerId.."-"..receipt.PurchaseId
	
	local success = nil
	
	pcall(function()
		success = PreviousPurchases:GetAsync(ID)
	end)
	
	if success then -- Has it already been bought ?
		-- Purchase has already been done
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	
	local player = game.Players:GetPlayerByUserId(receipt.PlayerId)
	
	if not player then
		-- Left, disconnected
		return Enum.ProductPurchaseDecision.NotProcessedYet -- We're going to give their rewards next time they join / next time fired
	else
		
		if receipt.ProductId == ONE_HUNDRED_CASH then
			-- They've bought 100 cash
			player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 100
		end

		pcall(function()
			PreviousPurchases:SetAsync(ID,true)
		end)
		return Enum.ProductPurchaseDecision.PurchaseGranted

	end
end

参考

編集できるプロジェクト
https://www.roblox.com/games/2184479937/Currency-system-video