All good! I would say this is the closest to the solution out of the posts so far.
But I don’t think anyone did a good job explaining and it still could be improved a bit, so I will try.
The problem was that you were only calling workspace:GetPartsInPart ONCE, right when your script first runs.
In your repeat loop, you are simply repeatedly printing the objects that were detected when the game started, which won’t detect any players that come into the part after the game first started. To fix this, you would put the workspace:GetPartsInPart INSIDE of the repeat block like so:
repeat
wait(1)
local objectsInSpace = workspace:GetPartsInPart(script.Parent)
print(objectsInSpace)
until nil
While this is good, there are a few things we can do to make the code cleaner. First, we use a while loop instead of a repeat loop.
while wait(1) do
local objectsInSpace = workspace:GetPartsInPart(script.Parent)
print(objectsInSpace)
end
Cool. Now it looks so much cleaner. But one thing to note is that wait() is deprecated, meaning that you should no longer use it. Instead, use task.wait() - it’s faster/more optimized and is the new standard. Try to remember this whenever you script. With this change, the resulting code will be:
while task.wait(1) do
local objectsInSpace = workspace:GetPartsInPart(script.Parent)
print(objectsInSpace)
end
Detecting the player is another process. First, you must get the detected part’s parent. Then, you should check if it has a player connected to it. You can do this by using game:GetService("Players"):GetPlayerFromCharacter(). Let’s add this in:
local Players = game:GetService("Players")
while task.wait(1) do
local objectsInSpace = workspace:GetPartsInPart(script.Parent)
print(objectsInSpace)
for _, part in objectsInSpace do -- simply iterating is faster than pairs or ipairs
local character = part.Parent
local player = Players:GetPlayerFromCharacter(character)
if not player then continue end
-- use player (u could also have a function that takes in the player and does wtv)
end
end
And that’s it! I hope that helped!