Hello. The problem is that you used an in pairs
loop which would run before the touched event fires which would mean the loop wouldn't find anything in the table so it would go on with the rest of the script. Instead, use a coroutine
with a while true do
loop which checks if a player is in the "playersIn" table. Also, I used a connection variable with :Disconnect()
to prevent other players getting added to the table. Try this:
03 | local string 1 = workspace.player 1 |
07 | local function onTouched(hit) |
08 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
09 | local player = game:GetService( "Players" ):GetPlayerFromCharacter(hit.Parent) |
10 | table.insert(playersIn, player) |
11 | connection:Disconnect() |
15 | connection = workspace.red.Touched:Connect(onTouched) |
18 | coroutine.wrap( function () |
21 | string 1. Value = playersIn [ 1 ] .Name |
Coroutines are a way to use code without yielding the rest of the script. Please upvote and accept this answer if it helped.