如何使用开发产品在 Roblox 中制作持续 30 秒的护盾?
How do I make a shield that lasts for 30 seconds in Roblox using a dev product?
当有人购买我的开发产品时,我希望他们得到一个持续 30 秒的可见护盾。
这是我试过的代码:
local mpService = game:GetService("MarketplaceService")
local Debris = game:GetService("Debris")
local function giveForcefield(player, duration)
local character = player.Character
if character then
local forceField = Instance.new("ForceField")
forceField.Visible = true
forceField.Parent = character
if duration then
Debris:AddItem(forceField, duration)
end
end
end
mpService.ProcessReceipt = function(purchaceInfo)
local plr = game:GetService("Players"):GetPlayerByUserId(purchaceInfo.PlayerId)
if purchaceInfo.ProductId == xxxxxxx then
game.Players.PlayerAdded:connect(function(plr)
repeat wait() until plr.Character
local char = plr.Character
giveForcefield(plr, 30)
local forceField = Instance.new("ForceField")
forceField.Visible = true
forceField.Parent = char
end)
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
我可以购买开发产品,但代码运行后没有任何反应。
我尝试了很多东西,但我有点迷路了。
我能得到一些帮助吗?
您编写的用于创建和销毁 ForceField 的两种方法都是有效的,但正如 Piglet 所建议的那样,您的代码根本没有被调用。你的代码是说,“一旦有人购买了这个产品,等待另一个玩家加入并进入游戏,然后给那个新玩家力场。”
通常,game.Players.PlayerAdded:connect(function(plr)
被用作访问 Player object, but you have already gotten the Player object by calling GetPlayerByUserId 的一种方式,因此您可以只使用它来访问角色模型。
顺便提一下,您应该只在产品已成功提供后才将其标记为 PurchaseGranted。通过将 PurchaseGranted 作为默认 return 状态,您 运行 冒着有人购买尚未配置的产品的风险,并且您最终会拿走他们的钱而不给他们任何东西 return.
local PlayerService = game:GetService("Players")
local Debris = game:GetService("Debris")
mpService.ProcessReceipt = function(purchaceInfo)
-- make sure that the player is still in the game
local plr = PlayerService:GetPlayerByUserId(purchaceInfo.PlayerId)
if not plr then
warn("Player could not be found. They might have left")
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if purchaceInfo.ProductId == xxxxxxx then
-- check if their character has spawned
local char = plr.Character
if not char then
warn("Could not find player's character in game")
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- grant the force field
local forceField = Instance.new("ForceField", char)
-- destroy the force field after a few seconds
local duration = 30
Debris:AddItem(forceField, duration)
-- mark this item as granted
return Enum.ProductPurchaseDecision.PurchaseGranted
end
-- not sure what thing they bought
warn("Unprocessed item receipt", purchaseInfo)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
当有人购买我的开发产品时,我希望他们得到一个持续 30 秒的可见护盾。
这是我试过的代码:
local mpService = game:GetService("MarketplaceService")
local Debris = game:GetService("Debris")
local function giveForcefield(player, duration)
local character = player.Character
if character then
local forceField = Instance.new("ForceField")
forceField.Visible = true
forceField.Parent = character
if duration then
Debris:AddItem(forceField, duration)
end
end
end
mpService.ProcessReceipt = function(purchaceInfo)
local plr = game:GetService("Players"):GetPlayerByUserId(purchaceInfo.PlayerId)
if purchaceInfo.ProductId == xxxxxxx then
game.Players.PlayerAdded:connect(function(plr)
repeat wait() until plr.Character
local char = plr.Character
giveForcefield(plr, 30)
local forceField = Instance.new("ForceField")
forceField.Visible = true
forceField.Parent = char
end)
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
我可以购买开发产品,但代码运行后没有任何反应。 我尝试了很多东西,但我有点迷路了。 我能得到一些帮助吗?
您编写的用于创建和销毁 ForceField 的两种方法都是有效的,但正如 Piglet 所建议的那样,您的代码根本没有被调用。你的代码是说,“一旦有人购买了这个产品,等待另一个玩家加入并进入游戏,然后给那个新玩家力场。”
通常,game.Players.PlayerAdded:connect(function(plr)
被用作访问 Player object, but you have already gotten the Player object by calling GetPlayerByUserId 的一种方式,因此您可以只使用它来访问角色模型。
顺便提一下,您应该只在产品已成功提供后才将其标记为 PurchaseGranted。通过将 PurchaseGranted 作为默认 return 状态,您 运行 冒着有人购买尚未配置的产品的风险,并且您最终会拿走他们的钱而不给他们任何东西 return.
local PlayerService = game:GetService("Players")
local Debris = game:GetService("Debris")
mpService.ProcessReceipt = function(purchaceInfo)
-- make sure that the player is still in the game
local plr = PlayerService:GetPlayerByUserId(purchaceInfo.PlayerId)
if not plr then
warn("Player could not be found. They might have left")
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if purchaceInfo.ProductId == xxxxxxx then
-- check if their character has spawned
local char = plr.Character
if not char then
warn("Could not find player's character in game")
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- grant the force field
local forceField = Instance.new("ForceField", char)
-- destroy the force field after a few seconds
local duration = 30
Debris:AddItem(forceField, duration)
-- mark this item as granted
return Enum.ProductPurchaseDecision.PurchaseGranted
end
-- not sure what thing they bought
warn("Unprocessed item receipt", purchaseInfo)
return Enum.ProductPurchaseDecision.NotProcessedYet
end