✓ Accepted answer
Problem
Players are not being teleported when your condition is met.
Solution
The first thing I noticed is you have an event inside of the same event. Don't do this.
Next, if you don't give the Teleport method a player object to teleport, nothing will happen to the player. (In the code below, I changed the method to TeleportPartyAsync)
The last thing is how you are incrementing the player_count variable. You have not confirmed that the 'hit' part is a descendant (or child) of the player's character.
Code
local players_service = game:GetService("Players")
local ts = game:GetService("TeleportService")
players_touching = {}
workspace:WaitForChild("Part").Touched:Connect(function(hit)
local player = players_service:GetPlayerFromCharacter(hit.Parent)
if player ~= nil then
if table.find(players_touching,player) then
table.insert(players_touching,player)
if #players_touching >= 2 then
ts:TeleportPartyAsync(7586111845,players_touching)
players_touching = {}
end
end
end
end)
workspace:WaitForChild("Part").TouchEnded:Connect(function(hit)
local player = players_service:GetPlayerFromCharacter(hit.Parent)
if player ~= nil then
local index = table.find(players_touching,player)
if index ~= nil then
table.remove(players_touching,index)
end
end
end)
players_service.PlayerRemoving:Connect(function(player)
local index = table.find(players_touching,player)
if index ~= nil then
table.remove(players_touching,index)
end
end)
I added 2 new events. The PlayerRemoving event which fires when a player leaves the game, and the TouchEnded event which fires when a BasePart stops touching another BasePart. When the player is no longer touching the part, we want to prevent them from being included in the teleport.
I also changed the 'player_count' variable which was a number to the 'players_touching' variable which is now a table that stores the player object value for each player we plan on teleporting.
Conclusion
If anything I have said that is confusing, you find any more bugs or you would like more help with another topic, you can contact me through my Discord server.
1 comment
This worked, thank you. I took everything that you said into consideration and tried to understand the code, which I did and it will help me avoid the same mistake in the future. I've learned some stuff from this too, Thank you~