hello scripters
I’m trying to detect the blocks close to the player, but I’m not talking about a specific block, I’m talking about any block, it can be a part or a model, I have no idea how to do it and thanks in advance
something like this
hello scripters
I’m trying to detect the blocks close to the player, but I’m not talking about a specific block, I’m talking about any block, it can be a part or a model, I have no idea how to do it and thanks in advance
something like this
set up a hitbox. Take a part and weld it to the player, set it too massless, make it transparent. Then just use .Touched
.
iterate over everything in workspace, then check the distance between the part and the player.
function checkClose(plr, maxDist)
closeBlocks = []
for i,v in pairs(game.workspace:GetDescendants()) do
local pos = nil
if v:IsA("BasePart") then
pos = v.Position
end
if v:IsA("Model") then
if v.PrimaryPart then
pos = v.PrimaryPart.Position
else
pos = v:GetChildren()[1].Position --get a part within the model to be the reference point
end
end
if pos then
if (plr.Character.Position - pos).Magnitude < maxDist then
table.insert(closeBlocks, v)
end
end
end
return closeBlocks
end
Something like this would be called every time you look for nearby parts, it returns a list of parts within a certain distance from the player.
I believe you can just use GetPartBoundsInRadius and filter out the player’s character so it detects everything but the player.
this works also! depends on if you want a physical hitbox or not.
I was thinking about recommending this but I didn’t for 1 reason:
It might cause performance issues, constantly looping through everything in the workspace. Especially if it’s a large game/map.
If at the time I thought the same, I’m still trying things
I already found the answer, I post this in case someone has the same question, the solution was the following
local camera = workspace.CurrentCamera
local plr = Player.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local rootPart = chr:WaitForChild("HumanoidRootPart")
while task.wait(0.25) do
local extents = 8 * Vector3.one -- 8 is Radius
local region = Region3.new(rootPart.Position - extents, rootPart.Position + extents)
local np = workspace:FindPartsInRegion3WithIgnoreList(region,{character,camera}) --Save nearby objects in a list
for _, p in np do -- check nearby objects one by one and print them
print(p)
end
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.