So I've been working on a story game, and I had released it today. But when I play with friends, I can't, because the game crashes for everyone when teleporting. It's become quite an issue, and I can't seem to figure out why it happens.
The script:
local truck = game.Workspace.Chapter1Lobby.Chapter1Truck local Seats = truck.Chapter1TeleportSeats:GetChildren() local exitGui = script.Parent:FindFirstChild("ExitButton") local debounce = false local event = game.ReplicatedStorage:WaitForChild("SendPlayersToChapter1") local loadingscreen = script.Parent:FindFirstChild("LoadingScreen") local plrs = {} script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") and debounce == false then local player = game.Players:GetPlayerFromCharacter(hit.Parent) table.insert(plrs, player) for i, emptySeat in pairs(Seats) do if emptySeat.Occupant == nil then player.Character.Humanoid.WalkSpeed = 0 player.Character.Humanoid.JumpPower = 0 player.Character.HumanoidRootPart.CFrame = emptySeat.CFrame exitGui:Clone().Parent = player.PlayerGui break end end end end) -- happens somewhere in this block of code if im correct event.Event:Connect(function(place) debounce = true wait() if #plrs > 0 then local tpService = game:GetService("TeleportService") local code = tpService:ReserveServer(place) wait() for i, v in ipairs(plrs) do tpService:TeleportToPrivateServer(place, code, {v}) wait() table.remove(plrs, i) wait() end end wait() debounce = false end)
Please help!
Edit: So I've seen some of the answers, and I think some clearing up needs to be done. First of all, there is NO ERROR in the script. My issue is that the client hangs upon teleporting, thus preventing them from getting into the actual chapter.
Also, I am not much of an experienced scripter. I started scripting back in March, and I have learned a LOT since, but I will occasionally need an example script to do things. My script above is just an edited version of the script in this video: https://www.youtube.com/watch?v=27nHlMvmbek (skip to 7:29)
Ok, so turns out the problem was Moon Animator. Disabling it when no longer needed fixed the issue! Thanks for all your suggestions anyways!
Your Teleporting people too much can get it to crash you. Your script is fine but the problem is you forgot just 1 thing.
local truck = game.Workspace.Chapter1Lobby.Chapter1Truck local Seats = truck.Chapter1TeleportSeats:GetChildren() local exitGui = script.Parent:FindFirstChild("ExitButton") local debounce = false local event = game.ReplicatedStorage:WaitForChild("SendPlayersToChapter1") local loadingscreen = script.Parent:FindFirstChild("LoadingScreen") local plrs = {} script.Parent.Touched:Connect(function(hit) if debounce then --Right here return end --Returns the whole script until its set to false if hit.Parent:FindFirstChild("Humanoid") and debounce == false then local player = game.Players:GetPlayerFromCharacter(hit.Parent) table.insert(plrs, player) for i, emptySeat in pairs(Seats) do if emptySeat.Occupant == nil then player.Character.Humanoid.WalkSpeed = 0 player.Character.Humanoid.JumpPower = 0 player.Character.HumanoidRootPart.CFrame = emptySeat.CFrame exitGui:Clone().Parent = player.PlayerGui break end end end end) -- happens somewhere in this block of code if im correct event.Event:Connect(function(place) debounce = true --cant do it again wait() if #plrs > 0 then local tpService = game:GetService("TeleportService") local code = tpService:ReserveServer(place) wait() tpService:TeleportToPrivateServer(place, code, plrs) wait() for i, v in ipairs(plrs) do table.remove(plrs, i) wait() end end wait() debounce = false --does it again
end) I'm sorry if i sound like im re explaining debounce but that should fix it if im sure Overall, everything is good if im correct.
I also agree with kepiblop, but there are some more things happening too with the script, from my understandings, you have to be careful when scripting debounce, what the script is doing is that it's forcing the player to teleport and it's forcing it atleast 300 times a second (I've experimented before and that's the usual tries). Debounce is critically important and fragile.
Solution: I'm not a huge expert in debouce, but I do believe that you should have A LOT of wait() variables, and also have a side-script to stabilize the system just in case it goes out of hand, mostly a script that watches the framerate, for example.
Watch framework, if it hits 40 frames or lower, begin function wait() Start by disabling the script, force-stop what it has already begun.
Yea not a huge expert on debounce, but I do make games! So I always have a stabilizer for big scripts like that, sorry if I wasn't too much helped.
Do the clients leave the original server? If not, print out the results of https://developer.roblox.com/en-us/api-reference/event/TeleportService/TeleportInitFailed and you might get a clue as to what's going on. If they do leave the original server but fail to load into your first chapter, I think it's more likely that you have a faulty script in your first chapter. You could test this by disabling literally all your scripts for the chapter and seeing if you can load in after that. If so, then it's just a matter of tracking down which script(s) are responsible.
Notes on the script you've displayed:
You assign to debounce
but never use it. The problem you need to solve is that a Touch event can occur multiple times from the same character and you don't want to act on that multiple times per player. If you use a simple debounce variable, only one player can touch the part at once. Better is to keep track of each player who has touched it like so:
local teleportingPlayers = {} script.Parent.Touched:Connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player and not teleportingPlayers[player] then -- Make sure that it's a player and that we aren't already planning on teleporting them teleportingPlayers[player] = true -- Record that we want to teleport them -- You can perform the rest of your code here end end)
What I just proposed would be fine, except then if they use the exit gui then you don't want to teleport them anymore. The optimal way to fix this would be to have your exitGui communicate with this script somehow so that it could perform teleportingPlayers[player] = nil
if they're no longer going to be teleported. (You would also want to perform that cleanup in response to the Players.PlayerRemoving
event.)
An alternate solution entirely is to let the list of seat occupants act as the list of players, like so:
local truck = game.Workspace.Chapter1Lobby.Chapter1Truck local seats = truck.Chapter1TeleportSeats:GetChildren() local exitGui = script.Parent:FindFirstChild("ExitButton") local event = game.ReplicatedStorage:WaitForChild("SendPlayersToChapter1") local loadingScreen = script.Parent:FindFirstChild("LoadingScreen") local function playerInSeats(player) local character = player.Character local humanoid = character and character:FindFirstChild("Humanoid") if not humanoid then return false end for i, seat in ipairs(seats) do if seat.Occupant == humanoid then return true end end return false end local function getPlayerListFromSeats() local players = {} for i, seat in ipairs(seats) do if seat.Occupant then table.insert(players, game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)) end end return players end script.Parent.Touched:Connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player and not playerInSeats(player) then for i, emptySeat in ipairs(seats) do if emptySeat.Occupant == nil then player.Character.Humanoid.WalkSpeed = 0 player.Character.Humanoid.JumpPower = 0 player.Character.HumanoidRootPart.CFrame = emptySeat.CFrame exitGui:Clone().Parent = player.PlayerGui break end end end end) local tpService = game:GetService("TeleportService") local debounce = false event.Event:Connect(function(place) if debounce then return end debounce = true local players = getPlayerListFromSeats() if #players > 0 then local code = tpService:ReserveServer(place) tpService:TeleportToPrivateServer(place, code, players, nil, nil, loadingScreen) end debounce = false end)
Further notes:
ipairs
is preferable to pairs
when iterating over a list (more efficient and it explains that you're expecting a list)debounce
around the event
firing - this is only necessary if the event might be fired multiple times before the server is reserved and the teleportation completesloadingScreen
you had into the TeleportToPrivateServer command.Sit
function which you might consider using (https://developer.roblox.com/en-us/api-reference/function/Seat/Sit), assuming you are using the Seat parts.