I placed a part in an area to detect whether a player has entered the area, but sometimes the collision cannot be detected when the player enters the block. The player needs to stop in the part before it can be detected. Why is this
Here is the code I used:
function isPlayerInZone(player,TouchPart)
local character = player.Character
if not character or not character.PrimaryPart then
return false
end
local detectionZoneCFrame = TouchPart.CFrame
local detectionZoneSize = TouchPart.Size
local charPosition = character.PrimaryPart.Position
local localCharPosition = detectionZoneCFrame:pointToObjectSpace(charPosition)
local halfSizeX = detectionZoneSize.X / 2
local halfSizeY = detectionZoneSize.Y / 2
local halfSizeZ = detectionZoneSize.Z / 2
return (localCharPosition.X >= -halfSizeX and localCharPosition.X <= halfSizeX and
localCharPosition.Z >= -halfSizeZ and localCharPosition.Z <= halfSizeZ)
end
for i,v in pairs(Tag) do
v.Touched:Connect(function(part)
local character = part.Parent;
local player = Players:GetPlayerFromCharacter(character);
if player ~= nil and isPlayerInZone(player,v) then
if waitPlayerList[player.UserId] == nil then
waitPlayerList[player.UserId] = player;
TouchPart[player.UserId] = v
end
end
end)
end
RunService.Heartbeat:Connect(function()
for _, player in pairs(waitPlayerList) do
local Part = TouchPart[player.UserId]
if Part then
if not isPlayerInZone(player,Part) then
TouchPart[player.UserId] = nil
waitPlayerList[player.UserId] = nil;
return
end
end
end
end)