我想在 gui 中使用这个脚本来检查玩家是否有工具。罗布洛克斯工作室

I want this script inside a gui to check if a player has a tool. ROBLOX STUDIO

我正在做一个Gui商店,在这个商店里你可以买到工具。如果玩家已经在他的物品栏[=20]中拥有它,我想让它不给工具=].我试图寻找答案,但找不到。

这是脚本:

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 次。为了获得完美的解决方案,您将需要检查玩家的模型并查看工具是否在其中。您还可以做一些事情,让玩家在进入商店之前需要卸下他们的工具。