How to know if the player is not standing on the part anymore (removing the player user id from the table if he is not touching the part anymore)


image

local SpotScreen = script.Parent
local ScreenText = SpotScreen.SurfaceGui.ScreenFrame.ScreenText
local PlayersTouching = {}
SpotScreen.Touched:Connect(function(Touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent) 
	if player and not table.find(PlayersTouching, player.UserId) then
		table.insert(PlayersTouching, player.UserId)
		ScreenText.Text = #PlayersTouching .. "/2"
		print(#PlayersTouching)
	end
end)

I made this script to change the text to 1/2 if 1 player touched the part and to 2/2 if 2 players touched it but I want to remove the player userid from the table if he is not touching the part anymore I tried using touchended event but it does not work
so I want to remove the player userid from the table if he is not touching the spotscreen anymore (not standing on it)
the main idea of all of this is making the spotscreen text changes to 1/2 if one player is standing on the spotscreen and changes to 2/2 if 2 players are standing on the spotscreen and 0/2 if there is no players standing on it

You could check on a loop, the distance of player to the touch thing, or check if player still inside the part on a loop too.
I think the idea would be creating a loop when a player touch that, and constantly check the distance or if player still inside the “collisionBox”, if not break the loop, and remove player from tables, and restore counter.

You could read the magnitude which is the distance from player to that part, or using GetParts

I do not want to use a loop for not making the game heavier

I suggest dont be afraid of loops, you have control over them to stop them, and you would be using only one for that.

If you dont want to try it, then use the Touch ended, which I did before and it works “acceptable”

touch ended does not works cause it is fires when the touch end not when the player is not standing on a part anymore

so it is almost fires at the same time touch event fire so the text is just changing to 0/2 and 1/2 if the player is walking on the spotscreen

I perfectly remember building a door that when touched, gives you a GUI, and when touch ended removes the GUI and I remember works nicely

If its not possible to do the correct set up for the Touch ended, then go for the loop, which is a good approach, and its not too heavy.
Whats the code you used for the touch ended?

SpotScreen.TouchEnded:Connect(function(TouchEnd)
	local player = game:GetService("Players"):GetPlayerFromCharacter(TouchEnd.Parent) 
	if player and table.find(PlayersTouching, player.UserId) then
		table.remove(PlayersTouching, table.find(PlayersTouching, player.UserId))
		ScreenText.Text = #PlayersTouching .. "/2" 
		print(#PlayersTouching)
	end
end)

This would works fine if the TouchEnded event fires when the player is not standing on the spotscreen anymore but it is fires when the touch event ends

How big is your collisionBox? I do not mean the thing in the floor where the player stands, I mean the invisible big box bigger than a player that the player touch and end touch to trigger your system

there is no I am doing this on the spot screen

robloxapp-20230116-1034016.wmv (2.5 MB)

I suggest creating one, so the touched events works better, do not rely on the thing on the floor, cause that could be touched by any part of the character as the feet, and you only want to receive one signal the one from the HumanoidRootPart, and HRP will never touch a thing in the floor, create a collision box, bigger than a player, and invisible and connect the touch events to that

I am not using HRP I am getting the player from the touch.part using get player from character then put the player user id in the table

And when one of the feets stop touching the part the touch end event triggers…
Thats why using the HRP is better even if you are handling the “double” first touch with a table, as a debounce, the player exist in table good, but when by walking one of the feet stop touching the part the end event triggers…

1 Like

yea you are right I just realized that ok I will try it out

1 Like

Thank you so much! :+1: :+1: :+1:

1 Like

Awesome! no problem 30chaaars :3

1 Like

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