I have a function that loops repeatedly to check if any player has stepped inside a chunk by detecting if that player’s cframe is within the bounds of the chunk. The problem I’ve been running into is the signal only fires for some chunks, while others can’t detect any players at all.
I would be grateful for any help, thank you.
local coro = coroutine.create(function()
self:CheckPlayers()
end)
coroutine.resume(coro)
self.CheckCoro = coro
self.PlayerEntered:Once(function()
coroutine.yield(coro)
coroutine.close(coro)
end)
function module:CheckPlayers()
while task.wait(2) do
for i,player in Players:GetPlayers() do
if not player.Character then continue end
local playerPosition = player.Character:GetPivot()
local xMin = math.min(self.xStart, self.xEnd)
local zMin = math.min(self.zStart, self.zEnd)
local xMax = math.max(self.xStart, self.xEnd)
local zMax = math.max(self.zStart, self.zEnd)
if (xMin <= playerPosition.X and playerPosition.X <= xMax) and (zMin <= playerPosition.Z and playerPosition.Z <= zMax) then
print(`{player.Name} has entered this chunk!`)
print(`x0 = {self.xStart}`)
print(`x1 = {self.xEnd}`)
print(`z0 = {self.zStart}`)
print(`z1 = {self.zEnd}`)
print(`PlayerX = {playerPosition.X}`)
print(`PlayerZ = {playerPosition.Z}`)
print(`-----------------`)
self.PlayerEntered:Fire()
end
end
end
end
So is that code that you posted in specific instances created with like a Chunk.new(xstart, xend, ystart, yend) so that each one has it’s own PlayerEntered event?
Because otherwise the problem could be that since Module scripts always return the same table when required the same server/client side then that PlayerEntered event would be shared between all chunks so with the “.Once” it would be disconnected for all chunks after firing the first time.
Interesting, is each of the “voxels” there a chunk? Or does each chunk consist of like a 64x64 grid of those voxels?
Are you only seeing it fire once or are you seeing it fire several times for different chunks seconds/minutes apart?
Also curious what the use case is going to be because otherwise it seems simpler to calculate the player’s Grid location by using your origin from which you generated the chunks and the size of the chunks.
coroutine.yield does not yield the coroutine passed to it. In fact, coroutine.yield takes no arguments. Coroutines operate via cooperative multitasking, meaning each coroutine, including your main thread, is only able to execute one at a time—whatever coroutine you may attempt to yield aside from your own is already yielding. Because of this fact, coroutine.yield solely yields the caller thread. This inhibits the closure of the CheckPlayers coroutine
Each chunk has 16 voxels and each voxel has a size of 12.
It didn’t occur to me that I could do what you said. At first I was trying to go for a more object-oriented approach (although my concept of it might be flawed), I will try to change my approach and see if it works, thank you for your help.
The reason why this was not working was because I forgot to multiply x and z values of the chunk to the actual size (each block has a size of 12, so each chunk would realistically have a size of 16 * 12). Massive thanks to anyone who’s replied on this thread, I learned a bit from every reply on here, so thank you.