Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

(SOLVED) Script causes client to hang most of the time?

Asked by 4 years ago
Edited 4 years ago

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)

SOLVED!

Ok, so turns out the problem was Moon Animator. Disabling it when no longer needed fixed the issue! Thanks for all your suggestions anyways!

3 answers

Log in to vote
2
Answered by
kepiblop 124
4 years ago

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.

1
I forgot to say that the thing that worked for me is making debounce true from start and doing if not debounce then return end then i set debounce to false then i set it to true and it works fine for me. kepiblop 124 — 4y
1
sry for the late reply, i had to go somewhere, but how do i fix the game hanging?? i mean, thank you for the debounce stuff, i guess, but how do i fix the game hanging? FirewolfYT_751 223 — 4y
0
i tried teleporting them one by one but it still hanged FirewolfYT_751 223 — 4y
0
hmm what do you mean by hanging do you mean it takes forever to load? kepiblop 124 — 4y
0
Oh i see. Try using a wait on for i, emptySeat in pairs(Seats) i dont think thats ocassionally the problem i think it may be the server usage. kepiblop 124 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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.

0
ummm i dont even know HOW to do the solution, how are you supposed to check the framerate?? FirewolfYT_751 223 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)
  • I set up 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 completes
  • I added that loadingScreen you had into the TeleportToPrivateServer command.
  • I haven't had experience with teleporting so I don't know if teleporting everyone at once is actually less stable, but since TeleportToPrivateServer accepts a list of players, I figure it shouldn't be too bad (but if it helps then use the loop you've already got).
  • Seats have a 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.
0
i am so confused rn FirewolfYT_751 223 — 4y
0
If you have any specific questions I will try to answer them chess123mate 5873 — 4y
0
whats happening with the teleportation?? i tried seeing what was the problem but i couldnt tell what was happening at all because the game would freeze BEFORE the players got in the first chapter, if they got in. FirewolfYT_751 223 — 4y
0
wait i think i know what the problem is... FirewolfYT_751 223 — 4y

Answer this question