Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get a list of players touching a part? (Answered)

Hello all, I am trying to get a list of players touching a part, which I would then teleport to a private server using ReserveServer (which I have already done). So I am trying to figure out how to get that list, and then clear it after the players teleport. Thanks!

-- ItzHiimnew

9 comments

0

I'm not sure you can get a group of players who are touching a part.

0

Maybe you could get each individual player and add them to a list?

0

What do you mean buy list?

0

by

0

Something like a table.

0

It's complicated. It really depends on what you want to do in particular. Could you maybe define why you want to do this?

0

So far, I have a lobby, and I want to teleport a specific group of players to a reserved server. I have the code for the teleport and the reserved server written out, but I need a part that players go into, get put into a table, and then get teleported with the rest of the people in that table.

0

^^ Just for clarification, this object they touch is like, in the doorway for a certain room or such?

0

Yeah. They go into a car, and then the car drives away into a tunnel, and the part would be inside of the tunnel.

3 answers

✓ Accepted answer
local Part = (insert the touching part directory) -- part which will be touched by the players
local Table

-- teleport the players

Table = Part:GetTouchingParts() --GetTouchingParts() returns a table
Table = nil -- clean the table

Notes:

-I, personally wouldn't rely on GetTouchingParts(), rather Region3 or calculating the distance between the player and the part, however, it's completely up to you.

-GetTouchingParts() will probably return character's feet, and since feet's parent is the character itself, use something like game.Players[Part.Parent.Name] to get the player controlling the character. Also, GetTouchingParts() will return left and right foot, so you gotta find a way to get the player only once.

3 comments

0

I know that the code is very simple, and the reason is that i would just give the code for you if wrote it entirely.

0

Thanks!

0

No problem. Just remember that you'll have to get around with the problems i wrote on notes alone, or else we're just giving code for you.

Before we begin, make sure you have a a model in workspaxe and one string value inside the model called Player.

local part = script.Parent
function onTouched(part)
    local player = game.Players:FindFirstChild(part.Parent.Name)
    if player then
           workspace.Players.Player:Clone()
workspace.Players.Player:Clone().Parent = workspace.Players
workspace.Players.Player:Clone().Value = player.Name
workspace.Players.Player:Clone.Name = player.Name
    end
end

part.Touched:Connect(onTouched)

now, all the players who touched is a value in workspace

Okay, now we need to telport them to a private VIP server

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")
 
local code = TS:ReserveServer(game.PlaceId) -- Returns a code
for i, players in pairs(workspace.Players.GetChildren()) do
    print(player.Value)
end
 
TS:TeleportToPrivateServer(game.PlaceId,code,players)

this may work, not tested

Full script

local part = script.Parent
function onTouched(part)
    local player = game.Players:FindFirstChild(part.Parent.Name)
    if player then
           workspace.Players.Player:Clone()
workspace.Players.Player:Clone().Parent = workspace.Players
workspace.Players.Player:Clone().Value = player.Name
workspace.Players.Player:Clone.Name = player.Name
    end
end

part.Touched:Connect(onTouched)

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")
 
local code = TS:ReserveServer(game.PlaceId) -- Returns a code
for i, player in pairs(workspace.Players.GetChildren()) do

    print(player.Value)
players:Destory()
end
 
TS:TeleportToPrivateServer(game.PlaceId,code,players)


1 comment

0

Thanks!

I see. First we want to detect the player touching the brick. So we insert a script into the brick, and we write:

local list = {}

script.Parent.Touched:Connect(function(part)

end)

We added a list, any players that touch the part will be added to this list.

When the touch event occurs, it will fire the function with the parameter "part". Next we confirm it's a player.

if part.Parent:FindFirstChild("Humanoid") then

end

This will confirm it's a player. It checks the part's parent, and if it finds the first child named "Humanoid" then it will return true. If it is a player then it will run the code after "then".

Now we add the player instance to the list.

list[#list+1] = game.Players:GetPlayerByCharacter(part.Parent).UserId

"list[#list+1]", #list means the amount of values inside of "list", and then the "+1" is necessary as it basically says "length of list plus 1, or insert a value to the end of the list". We then say that it is equal to the player userId. We do this by getting the player instance (For the userId) by the character instance. Which is where "GetPlayerByCharacter()" comes in handy, we give it the character instance (aka part.Parent) and it returns the player instance. Great! We have the player instance. Now we add ".UserId" to the end of that function and we have the userId stored. The rest is up to you!

Apologies for any mistakes. Let me know about them.

1 comment

0

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.