Lua 脚本在 Roblox Studio 中相互禁用
Lua Scripts disabling each other in Roblox Studio
我正在尝试在 Roblox(大亨)中制作游戏,考虑到这是我第一次制作游戏,我使用了一些预制脚本和模型以及 YouTube 教程。我现在的问题是我有两个脚本,它们都使用相同的名称 DevProductHandler,(我尝试更改它们的名称但问题仍然存在)而且似乎每次只有一个会 运行测试游戏。禁用一个脚本可以让另一个 运行 完全正常,同时启用两个脚本只会导致一个脚本工作,每次随机选择一个 运行。
其中一个脚本用于控制从 GUI 提示的购买产品,另一个脚本控制从您踩在上面的大亨按钮提示的购买。我已经尝试通过将一个脚本复制粘贴到另一个脚本之下来合并脚本,但它仍然只能使 GUI DevProductHandler 脚本工作。我不知道是否有一些重叠的变量或某些东西会导致一个脚本不允许另一个脚本 运行,但我认为这很可能是问题所在,尽管我不确定如何修复我是 Lua 和游戏创作方面的新手,所以我想知道是否有人可以帮助我找出问题所在。这些是脚本。
local Tycoon = script.Parent.Tycoons:GetChildren()[1]
script.Parent = game.ServerScriptService
local DevProducts = {}
local MarketplaceService = game:GetService('MarketplaceService')
for i,v in pairs(Tycoon:WaitForChild('Buttons'):GetChildren()) do
if v:FindFirstChild('DevProduct') then
if v.DevProduct.Value > 0 then
DevProducts[v.DevProduct.Value] = v -- the button
end
end
end
MarketplaceService.ProcessReceipt = function(receiptInfo)
for i,plr in pairs(game.Players:GetPlayers()) do
if plr.userId == receiptInfo.PlayerId then
if DevProducts[receiptInfo.ProductId] then
local obj = DevProducts[receiptInfo.ProductId]
local PlrT = game.ServerStorage.PlayerMoney:WaitForChild(plr.Name).OwnsTycoon
if PlrT.Value ~= nil then
--if PlrT.Value.PurchasedObjects:FindFirstChild(obj.Object.Value) == false then
local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(plr.Name)
Create({[1] = 0,[2] = obj,[3] = PlayerStats}, PlrT.Value.BuyObject)
--end
end
end
end
end
end
function Create(tab, prnt)
local x = Instance.new('Model')
Instance.new('NumberValue',x).Value = tab[1]
x.Value.Name = "Cost"
Instance.new('ObjectValue',x).Value = tab[2]
x.Value.Name = "Button"
local Obj = Instance.new('ObjectValue',x)
Obj.Name = "Stats"
Obj.Value = tab[3]
x.Parent = prnt
end
以上是走过按钮时出现提示的脚本。
old_fog = game.Lighting.FogStart
local MarketplaceService = game:GetService("MarketplaceService")
function getPlayerFromId(id)
for i,v in pairs(game.Players:GetChildren()) do
if v.userId == id then
return v
end
end
return nil
end
MarketplaceService.ProcessReceipt = function(receiptInfo)
local productId = receiptInfo.ProductId
local playerId = receiptInfo.PlayerId
local player = getPlayerFromId(playerId)
local productName
if productId == 1172271849 then
local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
if cashmoney then
cashmoney.Value = cashmoney.Value + 10000
end
elseif productId == 1172270951 then
local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
if cashmoney then
cashmoney.Value = cashmoney.Value + 100000
end
elseif productId == 1172270763 then
local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
if cashmoney then
cashmoney.Value = cashmoney.Value + 1000000
end
elseif productId == 1172272327 then
local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
if cashmoney then
cashmoney.Value = cashmoney.Value + cashmoney.Value
end
elseif productId == 1172273117 then
local char = player.Character
if char then
local human = char:FindFirstChild("Humanoid")
if human then
human.WalkSpeed = human.WalkSpeed + human.WalkSpeed
end
end
elseif productId == 1172273437 then
game.ServerStorage.HyperlaserGun:Clone().Parent=player.Backpack
elseif productId == 1172272691 then
game.ServerStorage.FlyingCarpet:Clone().Parent=player.Backpack
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
以上是从 GUI 提示购买的脚本。
As with all callbacks, this function should be set once and only once
by a single Script. If you're selling multiple products in your game,
this callback must handle receipts for all of them.
您正在两个脚本中实施 game:GetService('MarketplaceService').ProcessReceipt
。一个覆盖另一个。
我正在尝试在 Roblox(大亨)中制作游戏,考虑到这是我第一次制作游戏,我使用了一些预制脚本和模型以及 YouTube 教程。我现在的问题是我有两个脚本,它们都使用相同的名称 DevProductHandler,(我尝试更改它们的名称但问题仍然存在)而且似乎每次只有一个会 运行测试游戏。禁用一个脚本可以让另一个 运行 完全正常,同时启用两个脚本只会导致一个脚本工作,每次随机选择一个 运行。
其中一个脚本用于控制从 GUI 提示的购买产品,另一个脚本控制从您踩在上面的大亨按钮提示的购买。我已经尝试通过将一个脚本复制粘贴到另一个脚本之下来合并脚本,但它仍然只能使 GUI DevProductHandler 脚本工作。我不知道是否有一些重叠的变量或某些东西会导致一个脚本不允许另一个脚本 运行,但我认为这很可能是问题所在,尽管我不确定如何修复我是 Lua 和游戏创作方面的新手,所以我想知道是否有人可以帮助我找出问题所在。这些是脚本。
local Tycoon = script.Parent.Tycoons:GetChildren()[1]
script.Parent = game.ServerScriptService
local DevProducts = {}
local MarketplaceService = game:GetService('MarketplaceService')
for i,v in pairs(Tycoon:WaitForChild('Buttons'):GetChildren()) do
if v:FindFirstChild('DevProduct') then
if v.DevProduct.Value > 0 then
DevProducts[v.DevProduct.Value] = v -- the button
end
end
end
MarketplaceService.ProcessReceipt = function(receiptInfo)
for i,plr in pairs(game.Players:GetPlayers()) do
if plr.userId == receiptInfo.PlayerId then
if DevProducts[receiptInfo.ProductId] then
local obj = DevProducts[receiptInfo.ProductId]
local PlrT = game.ServerStorage.PlayerMoney:WaitForChild(plr.Name).OwnsTycoon
if PlrT.Value ~= nil then
--if PlrT.Value.PurchasedObjects:FindFirstChild(obj.Object.Value) == false then
local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(plr.Name)
Create({[1] = 0,[2] = obj,[3] = PlayerStats}, PlrT.Value.BuyObject)
--end
end
end
end
end
end
function Create(tab, prnt)
local x = Instance.new('Model')
Instance.new('NumberValue',x).Value = tab[1]
x.Value.Name = "Cost"
Instance.new('ObjectValue',x).Value = tab[2]
x.Value.Name = "Button"
local Obj = Instance.new('ObjectValue',x)
Obj.Name = "Stats"
Obj.Value = tab[3]
x.Parent = prnt
end
以上是走过按钮时出现提示的脚本。
old_fog = game.Lighting.FogStart
local MarketplaceService = game:GetService("MarketplaceService")
function getPlayerFromId(id)
for i,v in pairs(game.Players:GetChildren()) do
if v.userId == id then
return v
end
end
return nil
end
MarketplaceService.ProcessReceipt = function(receiptInfo)
local productId = receiptInfo.ProductId
local playerId = receiptInfo.PlayerId
local player = getPlayerFromId(playerId)
local productName
if productId == 1172271849 then
local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
if cashmoney then
cashmoney.Value = cashmoney.Value + 10000
end
elseif productId == 1172270951 then
local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
if cashmoney then
cashmoney.Value = cashmoney.Value + 100000
end
elseif productId == 1172270763 then
local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
if cashmoney then
cashmoney.Value = cashmoney.Value + 1000000
end
elseif productId == 1172272327 then
local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
if cashmoney then
cashmoney.Value = cashmoney.Value + cashmoney.Value
end
elseif productId == 1172273117 then
local char = player.Character
if char then
local human = char:FindFirstChild("Humanoid")
if human then
human.WalkSpeed = human.WalkSpeed + human.WalkSpeed
end
end
elseif productId == 1172273437 then
game.ServerStorage.HyperlaserGun:Clone().Parent=player.Backpack
elseif productId == 1172272691 then
game.ServerStorage.FlyingCarpet:Clone().Parent=player.Backpack
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
以上是从 GUI 提示购买的脚本。
As with all callbacks, this function should be set once and only once by a single Script. If you're selling multiple products in your game, this callback must handle receipts for all of them.
您正在两个脚本中实施 game:GetService('MarketplaceService').ProcessReceipt
。一个覆盖另一个。