Issue with touched and touch ended

So, i started making a roblox game, in which you survive a tornado, or you can chase storms, pretty similar to “Twisted”. And I made a system which prevents from getting hurt INSIDE a building, it’s a part which just sets the bool value inside a player to true, and if the tornado will run above the building(it needs to be an EF0 or EF1, else it will just kill you) and detect the value is set to true, it won’t hurt you. That part has a script, in which are two events, TOUCHED and TOUCHENDED. They just randomly fire when not supposed to, especially TOUCHENDED. Here’s the code i wrote:

local debounce = false
script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if debounce == false then
		if player then
			debounce = true
	        local Indoor2 = player:WaitForChild("Indoor")
			Indoor2.Value = true
		end
		wait(0.1)
		debounce = false
	end
end)	
local debounce2 = false
script.Parent.TouchEnded:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if debounce2 == false then
		if player then
			debounce = true
			local Indoor2 = player:WaitForChild("Indoor")
			Indoor2.Value = false
		end
		wait(0.1)
		debounce = false
	end
end)	

I also tried using region3 and magnitude, but this part is not a regular shape and there are many different houses across the map. Thanks for help and sorry for my english.

3 Likes

Where is this script located in? Is this the tornado or the safety part?
And please populate this script with print(flags) so we know what does and doesn’t run and when.
Thanks

2 Likes

This is a server script located in the safety part, should i tell you what is in the output?

1 Like

Yes. And please use Print flags so we know whats firing and when

1 Like

So, i don’t really see anything wrong in the output about this script, i think it’s the touch ended problem, every one says that it’s really buggy and I should not use it.

1 Like

Read about WorldRoot (workspace) to learn about the GetPartsInPart function, which returns a table. Loop through this table and check if the part is a limb in a character.

1 Like

I dont think this would be feasible since you would need to constantly check GetPartsInPart…which would probably mean using RenderStep or a tick loop. Very performance heavy

Yeah, I need a script that would not lag much

If people are saying its buggy, then you might have to go and use that GetPartsInPart function. Perhaps attaching a tick function to spawn() and running that whenever Touched() properly finds something. And when GetPartsInPart cant find it, you can immediately turn off the tick function.

It still very bad for performance, but I dont really see an easy option if roblox code is what causes problems

Alright, could you help me with scripting?

I havent tried it myself, but try this out and see how it goes

local debouncer= false
local SpawnedThread = nil
--Run this constantly as long as it thinks the player is touching it
local GetParts = function(hit)
	local PlayerModel = hit.Parent
	while true do
		wait()--you can modify how often it checks if the player is still touching here
		local AllTouchingParts = workspace:GetPartsInPart(script.Parent)
		local Flag=false
		for __,element in pairs(AllTouchingParts) do
			if element.Parent == PlayerModel then
				--Player is still touching
				Flag=true
			end
		end
		if Flag then
			--Player is no longer within safety zone
            game.Players:GetPlayerFromCharacter(hit.Parent):WaitForChild("Indoor").Value=false
			if SpawnedThread then
				SpawnedThread:close()
				SpawnedThread=nil
			end
			return
		end
	end
end
script.Parent.Touched:connect(function(hit)
	if not debouncer then
		debouncer=true
		if SpawnedThread then
			SpawnedThread:close()
			SpawnedThread=nil
		end
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local Indoor2 = player:WaitForChild("Indoor")
			Indoor2.Value = true
			SpawnedThread = task.spawn(GetParts,hit)
		end
		debouncer=false
	end
end)

It’s working but i don’t really know where should I put a line disabling the bool value.

Oh yea. i forgot about that part. Theres a comment that says “Player is no longer within safety zone” THAT would be when you change the bool

Alright, should i appeal to player again, like you did in the second function?

I tried to do this, but it doesn’t really work, it just spams the bool value few times, then stops at “false”

How are you spawning a bool value within the player?

Ill just show you the script:

local debouncer= false
local SpawnedThread = nil
--Run this constantly as long as it thinks the player is touching it
local GetParts = function(hit)
	local PlayerModel = hit.Parent
	while true do
		wait()--you can modify how often it checks if the player is still touching here
		local AllTouchingParts = workspace:GetPartsInPart(script.Parent)
		local Flag=false
		for __,element in pairs(AllTouchingParts) do
			if element.Parent == PlayerModel then
				--Player is still touching
				Flag=true
			end
		end
		if Flag then
			--Player is no longer within safety zone
                local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local Indoor2 = player:WaitForChild("Indoor")
			Indoor2.Value = false
			SpawnedThread = task.spawn(hit)
		end
            game.Players:GetPlayerFromCharacter(hit.Parent):WaitForChild("Indoor").Value=false
			if SpawnedThread then
				SpawnedThread:close()
				SpawnedThread=nil
			end
			return
		end
	end
end
script.Parent.Touched:connect(function(hit)
	if not debouncer then
		debouncer=true
		if SpawnedThread then
			SpawnedThread:close()
			SpawnedThread=nil
		end
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local Indoor2 = player:WaitForChild("Indoor")
			Indoor2.Value = true
			SpawnedThread = task.spawn(GetParts,hit)
		end
		debouncer=false
	end
end)

This isnt the problem. Somehow, you are spawning a BoolValue object within the player. How?

Im using a server script inside a server script service.

I think I just need to put something there where’s that comment.