Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

HElP! if ONE player purchases a Dev Product, how would the whole server receives it's benefit?

Hello! Recently, I've been trying to fix one of my scripts.

This script is linked to a Dev Product. When the product is bought, it is intended to give everyone in the server + 50 Tokens (including the player who bought it!).

But... when the player purchases the product, nothing happens!

I tested the script out originally so that only the player who bought it received the + 50 Tokens, which worked. But the script which is supposed to give every player in the server the + 50 Tokens didn't... (FYI, both scripts were placed in ServerScriptService)

This is the script which only gives one player the + 50 Tokens. (Script1)

local MarketplaceService = game:GetService('MarketplaceService')
local devproductId = 389417210 -- The ID of the dev product.

MarketplaceService.ProcessReceipt = function(receiptInfo)
	for i, player in ipairs(game.Players:GetChildren()) do
		if player.userId == receiptInfo.PlayerId then
			if receiptInfo.ProductId == devproductId then
				
				-- Receive tokens
				player.leaderstats.Tokens.Value = player.leaderstats.Tokens.Value + 50
			end
		end
	end
	return Enum.ProductPurchasedDescision.PurchaseGranted
end

After finding that the other script (script above) worked, I tried this script, which is the script that is supposed to give everyone in the server + 50 Tokens. (Script2)

local MarketplaceService = game:GetService('MarketplaceService')
local devproductId = 389417210 -- The ID of the dev product.
local players = game:GetService("Players")

MarketplaceService.ProcessReceipt = function(receiptInfo)
	for i, player in ipairs(game.Players:GetChildren()) do
		if player.userId == receiptInfo.PlayerId then
			if receiptInfo.ProductId == devproductId then
				
				-- Receive Tokens
				players.leaderstats.Tokens.Value = players.leaderstats.Tokens.Value + 50
			end
		end
	end
	return Enum.ProductPurchasedDescision.PurchaseGranted
end

I'm not sure if I'm missing something, or if something is wrong. I'm relatively new to scripting, so Script2 is probably completely wrong.

If anyone could help me out a bit, that would be great!

4 comments

0

Players is not a actual "player" its a Service

0

Use game:GetService("Players"):GetPlayers() get service is the best way to get a service and getplayers is the recommended way to get players

0

userId is deprecated, use UserId. You know that GetPlayerByUserId exists right?

0

Simply remove the check you're making: if player.userId == receiptInfo.PlayerId then

1 answer

That's easy, You need to make a for loop on the players.

for number, plr ipairs(game.Players:GetChildern()) do
	-- Receive token

	plr.leaderstats.Tokens.Value = plr.leaderstats.Tokens.Value + 50
end

For loops for the victory!

3 comments

0

Hope it helps!

0

It still has the same outcome. Nothing happens. :(

0

Wouldn't I need a variable for the 'plr'?

This discussion is preserved as it was. New answers, comments, and votes are closed.