Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

why does playerAdded events not work with Http Services that require a module script?

--local Trello = require(game.ServerScriptService.TrelloAPI);
--
--local Board = Trello:GetBoardID("Test")
 

game.Players.ChildAdded:connect(function(player) 
print "player"
	
	
end)
--print"works"

this code only works when The local variables are commented out, but when you uncomment them, the Code fails to even go inside the event function.

4 comments

0

Try putting the GetBoardId function in its own thread (coroutine or spawn function)

0

yet i need that to input player names... i could remove it t o solve it,but it would not help get the end results which simply prints the player name on trello when user is joined

0

Put the Trello and Board Variable inside of the Function maybe that would help.

0

the function does not work with the variables...

1 answer

✓ Accepted answer

Yielding

That function yields. It's annoying, we all hate it, but it yields. That means that the first Player will join by the time that the connection is ready. You have 2 options in this case, which is either lazy loading or cleaning up.


Cleaning up
No, that's not what it's really called

In this case, you are going to do to every player already in the game what you would have done when they joined. Valkyrie does this because there's not really a practical use for lazy-loading in its use-case.

-- init up here
-- Includes your require() etc

local function PlayerAdded(p)
	-- Do all your stuff here
end;

-- Leave this alone.
game.Players.PlayerAdded:connect(PlayerAdded);
for k,v in next, game.Players:GetPlayers() do
	spawn(function()
		PlayerAdded(v);
	end);
end

Lazy-loading
Deferred loading if it makes you happier

In this case, you actually run all of your code on the first time that the event happens. It takes a little bit of setting up on your end, because you have to decide what to lazy load and how.

local Board, OtherVariablesYouNeed;
local _init = false;
local function Lazy()
	local Trello = require(TrelloAPI);
	Board = Trello:GetBoardID("Test"); -- No local if you need to use it outside.

	_init = true;
end;
game.Players.PlayerAdded:connect(function(p)
	if not _init then Lazy() end;
	-- You can do stuff now.
end);

2 comments

0

What does yielding mean in lua, also what does in next feature do in lua. I was unable to find any answers through searching.

0

Yielding is waiting. next goes onto the next entry in a table.

This discussion is preserved as it was. New answers, comments, and votes are closed.