在 GUI 中检查玩家是否有一种工具 ROBLOX STUDIO

我正在制作一个 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) ```
点赞
用户15107943
用户15107943

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

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

function buy()
    -- 获取玩家背包中的所有物品(工具)
    local toolsinbackpack = player.Backpack:GetChildren()
    -- 获取玩家背包中的物品(工具)数量
    local numberoftools = table.getn(toolsinbackpack)
    local playerhasthetool = false
    for key = 1,numberoftools, 1 do
        -- 检查玩家背包中的工具是否与要购买的工具名称匹配。
        if toolsinbackpack[key].Name == tool.Name then
            -- 如果名称匹配,则停止循环并将变量设置为 true。
            playerhasthetool = true
            break
        end
    end
    -- 如果玩家有足够的钱并且没有该工具,则可以购买该工具。
    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 次的工具。为了达到完美的解决方案,您需要检查玩家的模型并查看该工具是否在其中。您还可以做一些事情,让玩家在访问商店之前需要卸下他们的工具。

2021-02-11 19:17:27