我希望这个脚本在gui中检查玩家是否有工具.ROBLOX工作室



我正在制作Gui工具。我想让不显示如果播放器已经在他的库存。我试图寻找一个答案,但我找不到一个。

脚本如下:

player = script.Parent.Parent.Parent.Parent.Parent.Parent
money = player.leaderstats.Cash 
price = 100 
tool = game.Lighting:findFirstChild("Bigger")

function buy()
if money.Value >= price then
money.Value = money.Value - price
local tool1 = tool:clone()
tool1.Parent = player.Backpack
local tool2 = tool:clone()
tool2.Parent = player.StarterGear

end
end
script.Parent.MouseButton1Down:connect(buy) ```

我找到的解决方案包括检查玩家背包中的每个道具,并检查它是否与工具名称匹配。下面是代码:

player = script.Parent.Parent.Parent.Parent.Parent.Parent
money = player.leaderstats.Cash 
price = 100 
tool = game.Lighting:findFirstChild("Bigger")

function buy()
-- get's all items(tools) in the player's backpack
local toolsinbackpack = player.Backpack:GetChildren() 
-- get's the number of items(tools) in the player's backpack
local numberoftools = table.getn(toolsinbackpack)
local playerhasthetool = false
for key = 1,numberoftools, 1 do
-- check's if the tool in the player's backpack matches the name of the
-- tool it want's to buy.
if toolsinbackpack[key].Name == tool.Name then
-- if the names match, the loop stops running and the variable is
-- set to true 
playerhasthetool = true
break
end
end
-- if the player has enough money and doesn't have the tool, it's allowed to
-- buy the tool.
if money.Value >= price and playerhasthetool == false then
money.Value = money.Value - price
local tool1 = tool:clone()
tool1.Parent = player.Backpack
local tool2 = tool:clone()
tool2.Parent = player.StarterGear

end
end
script.Parent.MouseButton1Down:connect(buy)

请注意,如果玩家在脚本运行时装备了工具(使用工具),它将不会显示在其背包中,并且他将能够购买该工具2次。但是玩家只能购买2次以上的工具。对于一个完美的解决方案,你需要检查玩家的模型,看看工具是否在里面。你也可以做一些事情,让玩家在进入商店之前需要卸下他们的工具。

相关内容

最新更新