Hello! I am new to scripting about 2-3 months in, i am sorry if this is not in the correct topic! Also this is my first topic ever.
-
What do you want to achieve? Keep it simple and clear!
When a player touches the TeleportPart they get teleported inside and a Value gets added to a Folder, when they press leave while still touching the TeleportPart it teleports them to the TeleportOutPart and deletes that Value from the folder. -
What is the issue? Include screenshots / videos if possible!
When touching the TouchPart and triggering the RemoteEvent which activates once you press the “Leave Button” at the same time it removes the “Value” from the folder however because i am touching the TouchPart at the same time it adds the “Value” back once it has been removed and it does NOT teleport the player to the “TeleportOut” part, instead it teleports BACK to the TeleportPart
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
i tried changing the debounce location, adding checks for when the player is inside etc… but maybe i didn’t do it well
local PlaceId = place id
local TeleportService = game:GetService('TeleportService')
local Players = game:GetService('Players')
local Folder = script.Parent:WaitForChild('Players')
local Debouncer = false
local PlayerCount = script.Parent:WaitForChild("PlayerCount"):WaitForChild("TextLabel")
local TextLabel = script.Parent:WaitForChild("BillboardGui"):WaitForChild("TextLabel")
local Ready = script.Parent:WaitForChild("Ready")
local Teleporting = script.Parent:WaitForChild("Teleporting")
local MaxPlayers = 2
local MinPlayers = 1
local TeleportPart = script.Parent:WaitForChild("TeleportPart")
local TeleportOutPart = workspace:WaitForChild("Teleporters"):WaitForChild("TeleportOut")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote1 = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("JoinLobby")
local Remote2 = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("LeaveLobby")
local timer = 20
function checkIfExist(Character)
for _, value in pairs(Folder:GetChildren()) do
if value.Value == Character then
return true
end
end
return false
end
function TeleportParty()
local Plrs = {}
for _, value in pairs(Folder:GetChildren()) do
if value then
table.insert(Plrs, Players:GetPlayerFromCharacter(value.Value))
end
end
local ReserveCode = TeleportService:ReserveServer(PlaceId)
TeleportService:TeleportToPrivateServer(PlaceId,ReserveCode,Plrs)
wait(5)
Folder:ClearAllChildren()
end
function UpdateGui()
PlayerCount.Text = tostring(#Folder:GetChildren())..'/'..tostring(MaxPlayers)
end
script.Parent.Touched:Connect(function(part)
if not Debouncer then
Debouncer = true
if part.Parent:FindFirstChild("Humanoid") and #Folder:GetChildren() <= MaxPlayers then
if not checkIfExist(part.Parent) then
if not Teleporting.Enabled then
local Tag = Instance.new('ObjectValue')
Tag.Value = part.Parent
Tag.Parent = Folder
part.Parent:SetPrimaryPartCFrame(TeleportPart.CFrame)
Remote1:FireClient(Players:GetPlayerFromCharacter(part.Parent))
print("Players teleported")
elseif Teleporting.Enabled == true then
print("Can't enter players are being teleported!")
end
end
elseif #Folder:GetChildren() >= MaxPlayers then
print("Player limit reached")
end
Debouncer = false
end
end)
Remote2.OnServerEvent:Connect(function(player)
for _,value in pairs(Folder:GetChildren()) do
if value.Value == player.Character then
value:Destroy()
player.Character:SetPrimaryPartCFrame(TeleportOutPart.CFrame)
end
end
end)
local characters = {}
Players.PlayerAdded:Connect(function(player)
player.CharacterRemoving:Connect(function(character)
characters[player] = character
end)
end)
Players.PlayerRemoving:Connect(function(player)
local character = characters[player]
if character then
for _, value in pairs(Folder:GetChildren()) do
if character == value.Value then
value:Destroy()
print("Value has been deleted")
else
print("Value still standing")
end
end
characters[player] = nil
end
end)
Folder.ChildAdded:Connect(UpdateGui)
Folder.ChildRemoved:Connect(UpdateGui)
while true do
wait(1)
for count = timer, 0, -1 do
wait(1)
TextLabel.Text = count
end
local playerCount = #Folder:GetChildren()
if #Folder:GetChildren() >= MinPlayers and #Folder:GetChildren() <= MaxPlayers then
Ready.Enabled = false
Teleporting.Enabled = true
print("Teleporting players...")
TeleportParty()
wait()
while #Folder:GetChildren() > 0 do
print("Waiting to teleport players...")
end
wait(1)
Ready.Enabled = true
Teleporting.Enabled = false
print("Teleportation completed, restarting countdown.")
end
end
I do not take credit for the script as i followed a tutorial
tutorial link : tutorial
Also i am sorry if i over-complicated it, i am not too good at explaining things, thanks in advance!