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
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
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
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…