So i want to make a part that is attached to the player and detects all other players inside it, i used region3, because i thought it would work good with the part, but i don’t know how to make the region3 follow the part. I tried doing a update function just to change the Region3.Cframe to Part.Cframe but the script just don’t work.
This is my script:
player = game.Players.LocalPlayer
local part = Instance.new("Part")
local weld = Instance.new("Weld")
local partcr = false
local region
local created = false
function makepart()
if partcr == false then
partcr = true
weld.Name = "circleweld"
weld.Part0 = player.Character.UpperTorso
weld.Part1 = part
weld.Parent = part
part.Size = Vector3.new(20,20,20)
part.Parent = player.Character
part.CanCollide = false
part.Transparency = 0.9
createregion()
end
end
function createregion()
region = Region3.new(Vector3.new(part.Position), Vector3.new(20,20,20))
end
function findpartsinregion()
local partsFound = workspace:FindPartsInRegion3(region, part)
for i, part in pairs(partsFound) do
if part.Parent:FindFirstChild("Humanoid") then
print("found:", part.Parent.Name)
local selectedplayers = part.Parent.Name
print("player selected")
end
end
end
function update()
region.Cframe = part.CFrame
end
while wait() do
makepart()
findpartsinregion()
update() --if i call this function the script stops working
end
Finding parts transiently in a rotated region is not possible, the engine only supports axis aligned FindPartsInRegion3. You could use a heavier weight approach like creating a temporary part and querying GetTouchingParts() on it which would allow you to query an arbitrary region but cost a lot more.
Here is what I use. I place this code inside of a localscript, inside of the StarterCharacterScripts in order to define a region that is based on a size and placed in front of the character in order to grab items in a BOX REGION in front of the character, such as picking up coins. The coins are placed inside of the CurrentCamera so they are only visible to that individual player. I also use FindPartsInRegion3WithWhiteList instead of FindPartsInRegion3 because I can whitelist the parts that are being checked for faster processing. This is not a complete solution, but will give you an idea of how to handle this. You can set the transparency of the part to a visible value to show the box you are checking.
local player = game.Players.LocalPlayer
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
local character = player.Character
local hmndrootpart = character:FindFirstChild("HumanoidRootPart")
local DoorTouched = false
-- wait for the player GUI to load
player:WaitForChild("PlayerGui")
-- define a regional visualization part
local regionSize = 6
local heightSize = 7
local visualizeRegion = Region3.new(Vector3.new(1,1,1), Vector3.new(regionSize,heightSize,regionSize))
-- print("VisualizePart Created")
local visualizePart = Instance.new("Part", game.Workspace.PlayerObjects)
visualizePart.Name = player.Name.."-visualizePart"
visualizePart.Anchored = true
visualizePart.Size = visualizeRegion.Size
visualizePart.CFrame = visualizeRegion.CFrame
visualizePart.Color = Color3.new(255,0,0)
visualizePart.Transparency = 1
visualizePart.CanCollide = false
game:GetService("RunService").Heartbeat:connect(function()
-- buffer slightly
wait()
-- move the part to the players position and adjust it based on the lookvector of the humanoid
visualizePart.Position = hmndrootpart.Position + hmndrootpart.CFrame.lookVector * (regionSize / 2)
local PlayerSearchRegionTest = Region3.new(visualizePart.Position-(visualizePart.Size/2),visualizePart.Position+(visualizePart.Size/2))
local parts = workspace:FindPartsInRegion3WithWhiteList(PlayerSearchRegionTest, {workspace.CurrentCamera}, 1000)
-- loop through the parts array and print the name of each part
for partIndex, part in pairs(parts) do
if part.Name == "CoinPart" then
-- Do processing here
end
end
end)