【问题标题】:How to run code when any object with the same name is touched触摸任何同名对象时如何运行代码
【发布时间】:2021-01-31 20:08:21
【问题描述】:

所以我正在尝试在 Roblox 上开发一款小型硬币收集游戏,并且对脚本编写非常陌生。基本上每 0.25 - 1.5 秒,一小部分从(-254, 2, -255)(基板的一个角)克隆到(254, 2, 255)(对面的角)。这行得通,但我试图遍历workspace 中名为coin 的每个对象,当一个对象被触摸时,运行代码(现在我只是试图破坏该对象,但我可能只是更新Coinsleaderstat)。它没有给我任何错误,它只是不起作用。我也找遍了整个互联网,找不到任何东西。

ServerScriptStorage 中的代码(生成多维数据集并且已经可以工作,但显示它以寻求帮助。):

local runservice = game:GetService("RunService")
local interval = math.random(0.25, 1.5)
local coin = game.ServerStorage.coin
local counter = 0
local x = math.random(-254, 254)
local z = math.random(-255, 255)

runservice.Heartbeat:Connect(function(step)
    counter = counter + step
    if counter >= interval then
        counter = counter - interval
        local copy = coin:Clone()
        copy.Parent = workspace
        copy.Position = Vector3.new(x, 2, z)
        x = math.random(-254, 254)
        z = math.random(-255, 255)
        interval = math.random(0.25, 1.5)
    end
end)

处理触摸的桌面脚本:

for _, v in pairs(workspace:GetChildren()) do
    if v.Name == "coin" then
        print("foo")
    end
end

我希望这足以提供帮助!

【问题讨论】:

  • 我建议你:1)确保第一个代码片段中的workspace与第二个相同,2)确保循环for _, v in pairs(workspace:GetChildren())至少运行一次,3) 在每次迭代中找出 v 是什么。
  • 我真的不明白这是什么意思或怎么做,所以请你再解释一下吗?

标签: lua roblox


【解决方案1】:

好吧,既然您是 roblox 脚本的新手,那么让我用可能对您有很大帮助的良好实践来回答您的问题。 首先,在这种情况下,您不需要使用 Heartbeat,而是可以简单地使用 while 循环或递归函数和简单的 wait()。 此外,您最好在工作区中创建一个“硬币”文件夹,以免检查其他对象

local waitTime = math.random(25,150)/100 --random time between 0.25 and 1.5
while true do  --forever loop
    wait(waitTime)   --waits desired time
    local coin = game.ServerStorage.coin:Clone() --cloning your coin
    coin.Parent = workspace.Coins --Coins folder
    coin.Position = Vector3.new(math.random(0,10),2,math.random(0,10)) --you must use your own position
    coin.Touched:Connect(function(hitPart) --here is the touched function
        local plr = game.Players:FindFirstChild(hitPart.Parent.Name) --check if the hitPart is part of a player
        if plr then 
            plr.leaderstats.Coins.Value =  plr.leaderstats.Coins.Value + 1--here you can increment your coins value in your own value
            coin:Destroy()--destroys the coin
        end
    end)
    waitTime = math.random(25,150)/100 --set a new random value to wait next
end

您还提到了有关在工作区中循环每个硬币的内容,这就是为什么我说最好创建一个单独的文件夹。所以我在 StarterPlayerScripts 中制作了一个本地脚本,代码如下:

local RunService = game:GetService("RunService") --service
RunService.RenderStepped:Connect(function() --function on every game frame
    for i,v in pairs(workspace.Coins:GetChildren()) do --loop on every coin
        v.Orientation = Vector3.new(v.Orientation.X,v.Orientation.Y+5,v.Orientation.Z) --increasing Orientation just on Y in order to rotate them 
    end
end)

我在 localscript 上执行此操作,因为这只是一种视觉效果,并且在服务器端快速发送这么多功能绝不是一个好主意。这是我为你制作的游戏: https://www.roblox.com/games/5842250223/Help-for-TextBasedYoutube 您可以编辑该地点。

换句话说,回答“当触摸任何同名对象时如何运行代码?” 创建对象时需要设置对象的功能。

编辑:在短时间内向服务器发送许多请求也不是一个好主意,我建议您每 2 到 3 秒或更长时间创建一个硬币。

【讨论】:

  • 当我说“循环”时,我的意思是运行循环遍历每个硬币的代码,但是,您在上面回答了它。谢谢!
  • 另外,你将如何添加一个可绑定的事件,以便你使用它来增加硬币?我想这样做是因为我要向其中添加 DataStore2。实际上 nvm,只是设法让它工作。
  • 使用您提供的代码让 DS2 工作并不太难,所以再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
  • 2021-05-05
  • 2012-11-03
  • 2021-05-13
相关资源
最近更新 更多