Touch event ends for no reason

Hi,

I’m having an issue with a script I’ve written. The script detects whether a player is touching the script’s parent using a TouchEvent, and it sets the player’s OnLobby value to true or false accordingly. However, I’ve noticed that when a player is using certain tools, the OnLobby value automatically changes to false. Can someone help me resolve this issue?

Heres Script


local touchObject = script.Parent

local function onTouch(otherPart)
	local character = otherPart.Parent
	local player = game:GetService("Players"):GetPlayerFromCharacter(character)

	if player and player:FindFirstChild("OnLobby") then
		player.OnLobby.Value = true
	end
end


local function onTouchEnded(otherPart)
	local character = otherPart.Parent
	local player = game:GetService("Players"):GetPlayerFromCharacter(character)

	if player and player:FindFirstChild("OnLobby") then
		player.OnLobby.Value = false
	end
end


touchObject.Touched:Connect(onTouch)
touchObject.TouchEnded:Connect(onTouchEnded)

Thank you!

Touch events are usually quite inaccurate, and in your case seems to be thinking the player has stopped touching the part even though there are still other limbs touching that part.

In this case I’d make a loop which uses Workspace:GetPartsInPart() at small intervals (~1 second), and for each part returned by that function, it will check if it is a child of a player’s character, and then add that player to a table.
At the end, loop through all the players currently in the game and set their OnLobby.Value based on whether they’re in the table or not. Then, clear that table ready for the next loop.

It’s not the most efficient but it gets the job done (in theory). The reason I use a table here is to prevent issues with any code that connects to OnLobby:GetPropertyChangedSignal("Value")

2 Likes

Thanks i tried using loop and table and now its working you helped me <3

1 Like

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