Why is ":GetPlayerFromCharacter" not working for me?

Hello, I’m trying to find players in a Region3. But only the characters are detected, not the players.

Script in workspace.SoundPart:

local Players: Players = game:GetService("Players")

local soundEvent: RemoteEvent = game:GetService("ReplicatedStorage").SoundEvent

local soundPart: Part = script.Parent

local vectorMin: Vector3 = Vector3.new(soundPart.Position.X - (soundPart.Size.X / 2), soundPart.Position.Y - (soundPart.Size.Y / 2), soundPart.Position.Z - (soundPart.Size.Z / 2))
local vectorMax: Vector3 = Vector3.new(soundPart.Position.X + (soundPart.Size.X / 2), soundPart.Position.Y + (soundPart.Size.Y / 2), soundPart.Position.Z + (soundPart.Size.Z / 2))

local region3: Region3 = Region3.new(vectorMin, vectorMax)

while true do
	local partsInRegion3 = workspace:FindPartsInRegion3(region3)
	
	local playersInRegion3 = {}
	
	for index: number, basePart: BasePart in partsInRegion3 do
		if basePart.Parent and basePart.Parent:FindFirstChildOfClass("Humanoid") then
			local character: Model = basePart.Parent :: Model --Character exists, I already printed it.
			local player: Player? = Players:GetPlayerFromCharacter(character) --Does not print?
			
			if player and not table.find(playersInRegion3, player) then
				table.insert(playersInRegion3, player)
			end
		end
	end
	
	print(playersInRegion3)
	
	task.wait(1)
end

the pairs/ipairs

for index: number, basePart: BasePart in pairs(partsInRegion3) do

Region3 is deprecated. Use overlapParams instead.

This literally does the same, but with a longer code…

This is not code review, I’m trying to fix my script. Region3 still works, I want to fix it first?!

use workspace:GetPartsInParts if you really want to save some lines

1 Like

Fixed this issue by using workspace:GetPartBoundsInBox, thank y’all for thinking along.

workspace:GetPartBoundsInBox is the optimal and supported solution.
The actual cause of your problem was using incorrect minimum and maximum vector values for your region 3.

p.s @BirdieI90 there no need to include pairs, as this is luau not lua

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.