Issue with touched and touch ended

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.

No it wouldnt work. It will cause errors and coroutine is improperly used. I know it can cause performance issues, but youre just gonna have to use a tick function with GetPartsInPart.

here. this only changes values when it detects a change or a missing player.

local AllPlayersInside = {}
function RecurssiveParent(Obj)
	local player =game.Players:GetPlayerFromCharacter(Obj)
	if player then
		return Obj
	elseif Obj.Parent ==workspace then
		return false
	else 
		return RecurssiveParent(Obj.Parent)
	end
end
while true do
	wait(.1)
	local AllTouchingParts = workspace:GetPartsInPart(script.Parent)
	local Flag=false
	for __,element in pairs(AllPlayersInside)do
		element.Value=false
	end
	for __,element in pairs(AllTouchingParts) do
		local PlayerObj=RecurssiveParent(element)
		if PlayerObj then
			if AllPlayersInside[PlayerObj.Name]==nil then
				local Player = game:GetService("Players"):GetPlayerFromCharacter(PlayerObj)
				Player:WaitForChild("Indoor").Value=true
				AllPlayersInside[PlayerObj.Name] = {Player=Player,Value=true}
			else
				AllPlayersInside[PlayerObj.Name].Value=true
			end
		end
	end
	for index,element in pairs(AllPlayersInside) do
		if not element.Value then
			element.Player:WaitForChild("Indoor").Value=false
			AllPlayersInside[index]=nil
		end
	end
end
1 Like

Everything is working right, thank you for your help!

1 Like

Of course i don’t close this post, anyone can write here if someone has better ideas.

I hope that will also help other people who don’t have access to developer forum.

A check every half second can do.

Doing a bigger number will ease up on lag, but allow players to have even bigger iframes

This is true, but I think this is better than having a laggy game. To reduce lag, you can add some overlap params so it doesn’t query what it shouldn’t have to.

Could you guys help me with another topic please?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.