在 roblox lua 中遇到 Region3 问题
Having issues with Region3 in roblox lua
我一直在尝试编写 Region3 脚本,当您在其中时,它会播放一首歌。但我遇到了 2 个问题,1 是它认为玩家总是在其中,而你玩家不在。第二个是脚本运行得如此之快以至于它在任何事情发生之前一直重复
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
您查询了 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
我一直在尝试编写 Region3 脚本,当您在其中时,它会播放一首歌。但我遇到了 2 个问题,1 是它认为玩家总是在其中,而你玩家不在。第二个是脚本运行得如此之快以至于它在任何事情发生之前一直重复
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
您查询了 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