【问题标题】:In Roblox I want to create.If the player has a Tool in backpack and touches brick.Give Player 2000 cash在 Roblox 中我想创建。如果玩家在背包里有一个工具并接触了砖块。给玩家 2000 现金
【发布时间】:2021-08-05 01:40:18
【问题描述】:

我想在 Roblox 中创建。如果玩家在背包里有一个工具并接触了砖块。给玩家 2000 现金和工具消失了。我已经为现金制作了排行榜,并且我的工具已经在服务器存储中。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    首先,您可以使用.Touched 事件开始,该事件会在每次触摸对象时触发。一个.Touched事件可以携带一个参数;碰到它的物体。例如,如果玩家 (R15) 踩到地板上的块,则撞击该部件的对象可能是Left Foot。以下是.Touched 函数的骨架:

    <Part>.Touched:Connect(function(hit)
         print(hit.Name) --As per the example above, this would print "Left Foot"
    end)
    

    由于任何物体都可以接触零件,但并非每个人都有背包,因此您需要首先检查撞击您零件的物体是否来自实际玩家。您可以通过查找Humanoid 来做到这一点,这将是所触及部分的兄弟。您可以通过添加:

    <Part>.Touched:Connect(function(hit)
         if hit.Parent:FindFirstChild("Humanoid") then
              print(hit.Name) --As per the example above, this would print "Left Foot"
         end
    end)
    

    现在我们已经验证了玩家是触动我们的角色,我们可以使用game.Players 的函数GetPlayerFromCharacter()Character 只是你在game.workspace 中的玩家,所以通过调用这个函数,它会从你的角色中获取game.Players 中的玩家。这类似于game.Players:FindFirstChild(&lt;Player&gt;.Name)。任何一个都可以,但我将在这个例子中使用第一个。添加到您的代码中,它现在应该如下所示:

    <Part>.Touched:Connect(function(hit)
         if hit.Parent:FindFirstChild("Humanoid") then
              local player = game.Players:GetPlayerFromCharacter(hit.Parent)
         end
    end)
    

    您可能知道,Backpackgame.Players 中玩家的直系子代,因此我们可以使用player.Backpack 找到我们的背包。现在我们可以执行与检查humanoid 相同的操作,以检查我们的tool

    <Part>.Touched:Connect(function(hit)
         if hit.Parent:FindFirstChild("Humanoid") then
              local player = game.Players:GetPlayerFromCharacter(hit.Parent)
              local Backpack = player.Backpack
              if Backpack:FindFirstChild("<Tool Name>") then
                   player.leaderstats.<Money>.Value += 2000 --Gives them 2000 cash
              end
         end
    end)
    

    如果您之前将该工具克隆到玩家的背包中,您可以通过Backpack.&lt;Tool Name&gt;:Destroy() 将其删除/使其消失。

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2020-11-01
      • 2018-09-14
      • 2020-11-27
      • 1970-01-01
      • 2022-10-05
      • 2021-10-17
      • 2022-10-24
      • 2021-08-23
      相关资源
      最近更新 更多