Script doesn't reach this line

Basically, I have a function that I call that returns a value, but the script that called the function doesn’t run after. I have no idea why this occurs, because it never happened before.

Here is the code:
Script 1:

game.Players.PlayerAdded:Connect(function(player)
	local data = storage.LoadData(player.UserId)
	print("Reached2") -- DOESN'T PRINT
    ... -- Other things
end)

Script 2 (ModuleScript):

function module.LoadData(id)
    ... -- Other stuff that runs
	print("Reached") -- THIS GETS PRINTED
	return data -- Goes back to Script 1
end

You forgot to require the module, try this:

game.Players.PlayerAdded:Connect(function(player)
    local module = require(storage.YourModuleNameHere)
	local data = module.LoadData(player.UserId)
	print("Reached2")
    ... -- Other things
end)

Maybe this would work :slight_smile:

“storage” is already defined as script 2, so that won’t work.

This is almost certainly an issue with the “other stuff that runs” bit of the module, provided it just now quit working like you said. Did you do anything to the module recently?

No. Also, if the script printed “Reached”, there is no way that the other things could’ve errored.

Based on what I’m seeing, you only created a variable. Is there any code in between the variable and the print?

Yeah, if Reached gets printed, then LoadData is fine. Reached2 isn’t getting printed though. Are you even sure PlayerAdded is getting called on the player? For example, you could print player or data. Printing and seeing what lines get reached or not isn’t sufficient enough to debug.

The function could just not be running at all because the player joined ahead of PlayerAdded getting fired. The proper way to account for this case is to connect the function to PlayerAdded as well as run it across any player already in the game, as follows:

local Players = game:GetService("Players")

local function playerAdded(player)
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end

Two reasons this can happen:

  • You’re testing in Studio. Studio initialises a few other things so the function may not connect fast enough before the player joins the server.

  • You have yielding functions before connecting to PlayerAdded, delaying the time it takes for the connection line to get reached.

Great, I’m going to try doing team test, but I’m pretty sure the script doesn’t get yielded before the .PlayerAdded connection gets set, and it never occurred before.

It was 100% a studio issue, I tried it in team test 5 times and it worked fine. Thanks for the help!