How would I print something on a player connecting?

Hey there everyone,

I’ve been working on a project of mine, but I am a bit stuck. I’m trying to get something to print when a player joins. Here is my full script:

-- Editable Features: 

local username = "FxllenCode" -- Username of whitelisted user goes here, used for remote event.

local allowed = 82738847 -- UserID of whitelisted user goes here.

local commandStart = "!startAlerts" -- You can change this to whatever you want.

local commandEnd = "!endAlerts" -- This one too.



-- Do not touch the lines below unless you know what your are doing.

local Players = game:GetService("Players")

local check = false

local enabled = false

local confirm = false



local cache = {}
function getUserIdFromUsername(name)
	-- First, check if the cache contains the name
	if cache[name] then return cache[name] end
	-- Second, check if the user is already connected to the server
	local player = Players:FindFirstChild(name)
	if player then
		cache[name] = player.UserId
		return player.UserId
	end 
	-- If all else fails, send a request
	local id
	pcall(function ()
		id = Players:GetUserIdFromNameAsync(name)
	end)
	cache[name] = id
	
	return id
	
	
end



	
function checkForPerms(name)
	
	local id = getUserIdFromUsername(name)
	
	if id == allowed then 
		
		check = true
		print("Sound Perms")
		print(id)
		
	else
		
		check = false
		print("No perms")
		print(id)
		
	end
	end	



function pausePerms(player)
	
	if enabled == true then
		
		
		confirm = false
		enabled = false
		
	end
	
	
end




function confirmPerms(player)
	
	if check == true then
		
		confirm = true
		
		while confirm == true do 
			
			enabled = true
			print("plsplspls")
			wait(5)
			
		end
	
		
	end
	
end



Players.PlayerAdded:Connect(function(player)
	checkForPerms(player.Name)
end)

game.Players.PlayerAdded:Connect(function(player) -- call the yes
	player.Chatted:Connect(function(message)
		if message == commandStart then
			
			confirmPerms(player)
			
			print("im not an idiot yay")
		end
	end)
end)

game.Players.PlayerAdded:Connect(function(player) -- stop calling the yes
	player.Chatted:Connect(function(message)
		if message == commandEnd then
			
			pausePerms(player)
			
			print("me not big dumb")
		end
	end)
end)


while enabled == true do
	
	if game.Players.PlayerAdded:Connect() then
	
		
	print("someone joined :oooooooo also i didnt fail me large happy")
		
		wait(4)
		
	end
	
	
	
	
end
	



print(check)
print(allowed)
print()

If possible, help would be appreciated! I am kind of a novice scripter, so please be sure to explain the solution in detail and give me all the little bits and pieces so I can learn!

Thank you so much! :grinning:

In a server script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    print(player.Name .. " has joined!")
end)

also, please fix the spaces. Its a little hard to read

Firstly, Roblox provides a built-in function that allows developers to get a player’s UserId.
Example:

game.Players.PlayerAdded:Connect(function(plr)
    local Id = plr.UserId
    — use the variable for whatever use.
end)

So, you can make a table about whitelisted UserIds.

Also, this is very inefficient for making a lot of admin commands. Check out AlvinBlox’s tutorial.

1 Like

I’m not making admin commands.

Not sure if that will work for what I’m doing. Refer to this, and my end goal is not just to print it but to fire a remote event on join. I’m using an if/then inside a loop. Here is the section in question:

if thats what you want you can just do:

game.Players.PlayerAdded:Connect(function()
    if enabled then
        --fire the remote event
    end
end
2 Likes

I’ll give that a shot, thank you. My only question is, because it’s not in a loop, can it still run multiple times? I need the remote event to fire every time a person joins the game.

yes, it runs each time a player joins.

1 Like

Is this meant to be a joke? You should watch scripting tutorials on related subjects before attempting this.

You used 2 player added.

-- // CONSTANTS \\ --
local PlayerService = game:GetService("Players")
local PlayersWithPermission = {100000, "SuperBob"} -- Username or UserID

-- // FUNCTIONS \\ --
local function CheckPerms(Player)
     return table.find(PlayersWithPermission, Player.Name) or table.find(PlayersWithPermission, Player.UserId) 
end

-- // EVENTS \\ --
PlayerService.PlayerAdded:Connect(function(Player)
	if CheckPerms(Player) then
   --Whatever u want here
    end
end)

A few tips:

  1. Always put your constants and functions at the top
  2. Use local variables, much faster and will not interfere with non-related items
  3. Event Connections do not need to be repeated, only use one

No, this was not meant to be a joke. I usually script in other languages, just giving this a try. I’m going to use my current script as it’s easier for me to understand and change. Thank you for the suggestions.

Ah, Ok. Understandable.

If you feel satisfied with the current answers/solutions, You should mark this post as solved

1 Like

Thought I did, my bad. Thank you again for the tips, I’ll keep it in mind when I re-write.