Help on fixing "Touched/TouchEnded" script

So I made a “Touched” and “TouchEnded” to detect a part that is currently touching a zone.

local safe = game.Workspace.safezone.safezoneRoot
local takedamage = true
local Lobby = game.Workspace.Lobby
safe.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) and takedamage == true then
		takedamage = false
		print(takedamage)
	end
end)

Lobby.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) and takedamage == true then
		takedamage = false
		print(takedamage)
	end
end)
Lobby.TouchEnded:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) and takedamage == false then
		takedamage = true
		print(takedamage)
	end
end)
safe.TouchEnded:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) and takedamage == false then
		takedamage = true 
		print(takedamage)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		while true do
			while char and takedamage == true do
				char.Humanoid:TakeDamage(math.random(0.5,1))
				wait(math.random(0.2,0.5))
			end
			wait()
		end
	end)
end)

for some reasons, after I came up using this for the bordered zone(which is a cylinder), it stops workings forward. While my old zone which is a square worked. Both has the same script.
Here are videos:
-Old zone:
https://streamable.com/ce2fml
-New zone:
https://streamable.com/0v66mz

Or is it just because Roblox updated Touched feature??
Or is it some other script interupting it?
Both scripts used in both videos are the same!
If you come up with any solution, please let me know. Thanks for viewing!

1 Like

TouchEnded and Touched aren’t really reliable for this type of things I’d suggest Region3s so you can detect players in that specific zone by making multiple regions that surround it, if the part that you use for the blue barrier is an union that’s probably the reason it’s not working, unions aren’t totally reliable with collisions.

1 Like

In addition to what @jcnruad900 said, .Touched and .TouchEnded aren’t really reliable and are more memory extensive than a few other methods. Personally, I use raycasting for detecting which zone a player is in as it is (in my opinion) the most seamless and least memory extensive method.

2 Likes

well the its a meshpart so region3 isnt a solution. The previous zone is in circle shape and region3 is a blocky shape.

Hey I’d love to help, with this .Touched can be very unreliable so what I would do is have a Bool Value inside of the player that is set to true when the player is inside of that zone, then set to false upon walking out. After that you can have a script (perhaps in server script service) that is on a “while true do loop” where it constantly checks if the variable is true and if it is then the local player takes damage. Here is an example:

local isInZone = -- put wherever the bool value will be located here

while true do
   wait(0.1) -- reduces lag
   if isInZone.Value = true then
         game.Players.LocalPlayer -- put your hurt code here
   end
end

hmmm can you extend the idea? I have no clude how to do that. Would we use magnitude with it?

Will try that out. LocalScript right?

isnt that familiar with what am I doing?

The value changing script should most likely be a server script, and the while true do script should be a server script.

Uhm, not to be mean but it’s the same as what am I doing

I provided the code in the post above, try to take a look and please view, suggest me what to do.

My apologies, I did not notice. What I would do is keep the Bool Value idea in mind, but don’t use .Touch events from the player to activate it, since there are so many ways that can fail. One way to fix this is to create and weld an invisible hitbox around the player that activates it upon touching the zone since it is just one part it will have much better results. Sorry for the misunderstanding, hope this helps!

Are you using Precise collisions if it’s a mesh part?

I did a test with the “Lobby” which is standard big zone first then move on with the meshpart, both doesnt work.

as soon as 4 seconds pass by, everything stops working(the touch script). Even if I re-enter the zone or leave it, it doesnt even print.

Did you try mine? Just wondering.

from your idea, im thinking of getting just 1 body part within the player

That is also a very nice idea, I think that can work too.

You can check if the player distance is between MinDistance and MaxDistance if yes then damage it slowly.

Roblox’s touch system isn’t very effective. I would recommend using a raycast instead.