So for an hour I’ve been trying to find out why I can’t detect a player’s character using the GetPlayerFromCharacter() function. I know how to detect it without using this but I want to learn how to detect a player using that function with a for loop.
Here is my code:
local pos1 = workspace.pos1
local pos2 = workspace.pos2
local found = false
local myPart = workspace.test
local region = Region3.new(pos1.Position,pos2.Position)
local p = Instance.new("Part",workspace) --Region3 visualizer
p.Anchored = true
p.Transparency = 0.5
p.CanCollide = false
p.Size = region.Size
p.CFrame = region.CFrame
local IgnoreList = {p,pos1,pos2}
local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region,IgnoreList,100)
for _,part in pairs(partsInRegion) do -- loop through parts until player is one of them
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player then
print("Found player in region3!")
end
end
If you can explain this to me in a simple way that’d be nice, I want to understand why this isn’t working.
No, After I run the script and I go inside of my region3 nothing even comes up on my output. It is supposed to print “found player in region” but it isn’t
remember i want to make this script work using the GetPlayerFromCharacter function also I tried that a few seconds ago that doesn’t print anything either
I think it might be because your not constantly checking if there is something inside of the region,
if that didnt make sense then maybe this will help you understand
Before your player can even get into the region, the script runs so fast that it didnt detect your player, if im right it would only detect something that was already inside of it
edit: So try putting a part inside of the region in studio then run the game and see if it works
while true do
local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region,IgnoreList,100)
for _,part in pairs(partsInRegion) do -- loop through parts until player is one of them
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player then
print("Found player in region3!")
end
end
game:GetService("RunService").Heartbeat:Wait()
end
So I tried your code and it worked perfectly fine for me so your code is fine.
I assume either you’re not in the region when this script runs or you have more than 100 parts in the region so it doesn’t return the character parts(Which is unlikely).
Yeah thats exactly why it worked because what he was doing was running the script before he was in the region and it didnt have enough time to recognize him in the region
I both used the studio command bar and put it in a script and set disabled property to false then set it to true when I started the test.
Of course you can put it in a function and connect that function to an event that you can fire at any given time or just put a delay at the start of the script so you can have time to go to the region.
But yeah, you will need to put it in some sort of loop if you want constant detection.
So what is the loop that works for this? I used the for loop to keep checking until player was one of them and it would print in the output but it didn’t.