Okay, so I was making a system that if a player is in the lobby, a certain value inside the character becomes true and becomes false when the player isn’t inside. I did this by surrounding the lobby by parts and using Touched and TouchEnded to set the value to true or false.
Following is the code:
local regions = game.Workspace.regions:GetChildren()
for i, Part in pairs(regions) do
Part.Touched:Connect(function(hit)
local e = hit.Parent:FindFirstChild("DoingSomething")
if e then
if e.Value == true then return end
e.Value = true
end
end)
Part.TouchEnded:Connect(function(hit)
local e = hit.Parent:FindFirstChild("DoingSomething")
if e then
if e.Value == false then return end
e.Value = false
end
end)
end
THE PROBLEM:
Everything worked fine with only one part. Since The lobby is not something you can surround with one part, I put multiple parts and it didn’t work as intended. When the character switches from one part to another, the touched event won’t fire until the player jumps. These are the parts covering the lobby:
I have a local script constantly printing the value of the “DoingSomething” which is the value that is supposed to change when the player touches the part. The parts using .touched
This is shown in the video:
I tried searching around but couldn’t find something that helped. I also tried using “BasePart:GetPartsinPart” but it didn’t seem to work as intended for more than one player. I can provide the code which used getpartsinpart if requested.
Touched and TouchEnded will fire whenever any of a character’s limbs touch/stop touching a part respectively, you may want to check if the touching/not touching part has a particular name before continuing the function’s execution.
I highly advise against using Touched events when dealing with zones and large parts in particular, you can use an alternative way that is much more precise and accurate, workspace:GetPartsInPart(part, overlap params)
example:
local touched = {}
while task.wait() do
local zone = workspace:GetPartsInPart(part)
for i,v in pairs(zone) do
if v.Parent:FindFirstChild("Humanoid") and not touched[v.Parent.Name] then
-- Adding debounce
touched[v.Parent.Name] = v.Parent
-->> Code
-- Stuff
--<<
-- Removing debounce
task.delay(2, function()
if touched[v.Parent.Name] then
touched[v.Parent.Name] = nil
end
end
end
end
end
Zoneplus doesn’t work. The same goes with region3.
I used to following code for ZonePlus:
local part = game.Workspace.regions:GetChildren() -- the region in which players are to be detected
local ZonePlus = require(game.ReplicatedStorage.Modules.Zone)
for _, container in ipairs(part) do
local zone = ZonePlus.new(container)
zone.accuracy = ZonePlus.enum.Accuracy.High
zone.playerEntered:Connect(function(player)
print(("%s entered the zone!"):format(player.Name))
local char = player.Character
if char then
char:FindFirstChild("DoingSomething").Value = true
end
end)
zone.playerExited:Connect(function(player)
print(("%s exited the zone!"):format(player.Name))
local char = player.Character
if char then
char:FindFirstChild("DoingSomething").Value = false
end
end)
end
It works fine normally, but it has trouble detecting when the player switches from one part to another. Mostly, it works fine with the part switching but it sometimes just doesn’t detect when the player has entered the other zone. It just runs playerexited code with the last zone.
Region3 also has the same problem and I can’t figure out how to execute some code when the player isn’t in the region3.