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

How can I make a more accurate system to detect if a player is in a game?

Currently, I have a script that adds a value called "InGame" to the player once they join, with the value as false. Once layers are teleported to the map, I run this (In a module script):

function module:CollectPlayers()
		for _, Player in pairs ( game.Players:GetPlayers()) do
			table.insert(module.Playing, Player)
		end
	return module.Playing
end

for i, player in pairs(players) do
		if player then
			
			character = player.Character
			
			if character then
				
				--I run some other unimportant code here...

				player:FindFirstChild("InGame").Value = true
			end
		end
	end
end

Outside of the module script, I also have this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			player:WaitForChild("InGame").Value = false
			player:LoadCharacter()
		end)
	end)
end)

Thi sis quite messy, as it is spread out across three scripts. I was thinking of just putting a while true do loop that constantly checks to see if a player has a morph in them (In the game they get morphed into tornadoes) then set their value based on that, but there still has to be a more efficient way to do things. Any suggestions?

1 answer

I don't think you would actually need to do anything. But you could create a folder in workspace with the player's characters in it upon joining, or make a folder with values and the value's names are player names that join.

game.Players.PlayerAdded:Connect(function(player)
-- this code already gets any players that join and u can use player to connect to that player.

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