【问题标题】:Having issues with Region3 in roblox lua在 roblox lua 中遇到 Region3 的问题
【发布时间】:2021-11-06 11:03:49
【问题描述】:

我一直在尝试编写 Region3 脚本,当你在其中时,它会播放一首歌曲。但是我遇到了 2 个问题,第一个是它认为玩家总是在其中,而您却不在。第二个是脚本运行速度如此之快,以至于在任何事情发生之前它都会不断重复

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("THE SCRIPT WORKS!")
    end

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    您查询了Region 中的对象,但从未使用过结果而只是继续。循环 burhj 并检查有效部分。

    建议In this forum使用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
    

    或者,you can directly use the player position,如果玩家对象或位置已知:

    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
    

    辅助函数(如果不存在)可能会有所帮助。

    对于第二个问题,如果玩家在该地区,则设置一个标志。在尚未设置标志时播放声音。如果您离开该地区,请取消设置标志。

    --here are your vars
    local enteredFlag = false
    while true do 
        wait()
        if playerIsWithinRegion then --here come whatever approach you chose earlier
            if not enteredFlag then
                enteredFlag = true
                local song = game.Workspace.bb
                song:Play()
                print("THE SCRIPT WORKS!")
            end
        else
            --no player is in the region, lets reset the flag
            enteredFlag  = false
        end
    end
    

    【讨论】:

    • 您能添加建议的代码吗?
    • 完成!如果脚本包含错误/不推荐使用的代码,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 2015-03-04
    • 2018-12-01
    • 2017-04-21
    • 2020-10-27
    • 2018-12-23
    • 2018-12-29
    • 2020-10-28
    相关资源
    最近更新 更多