Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 我希望这个脚本在gui中检查玩家是否有工具。罗布罗克斯工作室_Lua_Roblox - Fatal编程技术网

Lua 我希望这个脚本在gui中检查玩家是否有工具。罗布罗克斯工作室

Lua 我希望这个脚本在gui中检查玩家是否有工具。罗布罗克斯工作室,lua,roblox,Lua,Roblox,我正在制作一个Gui商店,在这个商店里你可以买到工具。如果玩家已经在他的目录中有工具,我想让它不要给工具。我试着寻找答案,但找不到答案 以下是脚本: player = script.Parent.Parent.Parent.Parent.Parent.Parent money = player.leaderstats.Cash price = 100 tool = game.Lighting:findFirstChild("Bigger") function buy(

我正在制作一个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次。对于一个完美的解决方案,你需要检查玩家的模型,看看工具是否在里面。您还可以做一些事情,以便玩家在进入商店之前需要取消装备他们的工具。

好的,谢谢:)不客气(: