【发布时间】:2021-08-05 01:40:18
【问题描述】:
我想在 Roblox 中创建。如果玩家在背包里有一个工具并接触了砖块。给玩家 2000 现金和工具消失了。我已经为现金制作了排行榜,并且我的工具已经在服务器存储中。
【问题讨论】:
我想在 Roblox 中创建。如果玩家在背包里有一个工具并接触了砖块。给玩家 2000 现金和工具消失了。我已经为现金制作了排行榜,并且我的工具已经在服务器存储中。
【问题讨论】:
首先,您可以使用.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(<Player>.Name)。任何一个都可以,但我将在这个例子中使用第一个。添加到您的代码中,它现在应该如下所示:
<Part>.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
end
end)
您可能知道,Backpack 是game.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.<Tool Name>:Destroy() 将其删除/使其消失。
【讨论】: