【问题标题】:I want this script inside a gui to check if a player has a tool. ROBLOX STUDIO我希望这个脚本在 gui 中检查玩家是否有工具。机器人工作室
【发布时间】:2021-05-15 11:43:41
【问题描述】:

我正在做一个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) ```

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-08-23
      • 2021-12-03
      • 2022-10-24
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 2015-05-16
      相关资源
      最近更新 更多