在 roblox lua 中遇到 Region3 问题

我一直在尝试编写一个 Region3 脚本,在其中,当你在其中时,它会播放一首歌。 但我遇到了两个问题,第一个是它认为玩家总是在其中当你不在其中。 第二个问题是脚本运行得太快了,在任何事情发生之前就一直重复

local RegionPart = game.Workspace.RegionArea
local pos1 = RegionPart.Position - (RegionPart.Size / 2)
local pos2 = RegionPart.Position + (RegionPart.Size / 2)
local Region = Region3.new(pos1, pos2)

while true do
    wait()
    local burhj = workspace:FindPartsInRegion3(Region, nil, 1000)
    local song = game.Workspace.bb
    song:Play()
    print("脚本有效!")
end

原文链接 https://stackoverflow.com/questions/69123965

点赞
stackoverflow用户10391157
stackoverflow用户10391157

你查询了在 Region 中的对象,但从未使用结果并继续了下去。循环遍历 burhj 并检查有效的部分。

这个论坛中建议使用 FindFirstChild

for i,v in ipairs(burhj) do
    local player = v.Parent:FindFirstChild("Humanoid")
    if player then
       print("player is in region: " + player.Parent.Name)
    end
end

或者,如果已知玩家对象或位置,则可以直接使用玩家位置

local pos = yourPlayer.HumanoidRootPart.Position
local center = region.CFrame
local size = region.Size
if pos.X > center.X - size.X / 2 and pos.X < center.X + size.X / 2 and ... then
   print("player is in region")
end

如果不存在帮助函数,则可提供帮助。

对于第二个问题,如果玩家在区域内,则设置标志。当标志尚未设置时播放声音。离开区域时取消设置标志。

--以下为你的变量
local enteredFlag = false
while true do
    wait()
    if playerIsWithinRegion then --这里需要用到之前选择的方法
        if not enteredFlag then
            enteredFlag = true
            local song = game.Workspace.bb
            song:Play()
            print("THE SCRIPT WORKS!")
        end
    else
        --没有玩家在区域内,我们将重置标志
        enteredFlag  = false
    end
end
2021-09-10 08:42:37