How do I make it so a player can only join with a specific T-shirt?

Hey everyone,

I’m trying to find out how a player can join my game ONLY if they have on a specific T-shirt, (eg - Player joins game > game checks if they have required T-shirt > no T-Shirt > kick + a kick message > yes T-shirt? > stay) I’ve looked around the devforum for a bit, (couldn’t find any solutions), I’ve tried my hand at coding, (I’m kind of mediocre so it’s a bit challenging), anyone have an idea? (if im missing something in the question, let me know and ill keep it in mind for next time)

Thanks everyone!

It’s pretty simple honestly. All you need to do is whenever a player joins the server, just check if they have a shirt, and whether the shirt id matches, and then kick them if not.

Players.PlayerAdded:Connect(function(plr)
	local character = plr.CharacterAppearanceLoaded:Wait()
	
	local shirt = character:FindFirstChildOfClass("Shirt")
	
	if not shirt then
		plr:Kick("i'm sorry that you got kicked")
		return
	end
	
	if shirt.ShirtTemplate ~= "whatever shirt id you want them to wear" then
		plr:Kick("i'm sorry that you got kicked")
	end
end)

Once you understand why this works and what not, you can make it more tailored to your needs.

1 Like

Maybe I’m misunderstanding but - where would I add the data for what the T-shirt Id is? Secondarily, in the console it’s having this error; Workspace.Script:1: attempt to index nil with ‘PlayerAdded’
Third; I apologize for the late response; I was trying to work through it without hassling you.

Thank you for your time.

-- ServerScriptService/ForceTShirt.lua
local Players = game:GetService("Players")

-- replace this with your T-Shirt asset ID
local TSHIRT_ID = 1234567890  

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		-- wait for the HumanoidDescription to be available
		local humanoid = character:WaitForChild("Humanoid")

		-- create TShirt
		local tshirt = Instance.new("ShirtGraphic")
		tshirt.Name = "ForcedTShirt"
		tshirt.Graphic = "rbxassetid://" .. TSHIRT_ID
		tshirt.Parent = character
	end)
end)

Thanks for the response, but there is some things I’d like to clarify,

For one; I’d like the player to be kicked if they don’t have the correct T-shirt, from what I’m understanding your script is just changing it? Maybe I’m mistaken? Secondarily, even with that idea in mind your script doesn’t seem to be doing too much, to my understanding again, if I’m wrong please feel free to correct me.

Thank you for your response, I apologize if what I’m asking for is too much but I really am not sure how to get this done, I’ve tried inserting your script within ServerScriptService and Globally, but either or doesn’t work.

sorry I didn’t read it right its fine you just need to use humanoid descripton and check if there t-shirt id is == to the tshirt id you want

local Players = game:GetService("Players")
local REQUIRED_DECAL_ID = 1234567890 -- put your T-Shirt decal ID here
local KICK_MESSAGE = "You must be wearing the required T-Shirt to play."

local function getId(content)
	return tonumber(string.match(content or "", "%d+"))
end

local function wearing(char)
	local sg = char:FindFirstChildOfClass("ShirtGraphic")
	return sg and getId(sg.Graphic) == REQUIRED_DECAL_ID
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(char)
		if not wearing(char) then
			player:Kick(KICK_MESSAGE)
		end
	end)
end)

lololol chatgpt, only ai adds

-- ServerScriptService/ForceTShirt.lua

to the top of the code

1 Like

heheheehehehehee you know ball i understand it tho

Regardless; the code doesn’t work, I do appreciate the help - really, I do, but where would you recommend to look to find somewhat of a solution to this? I’ve noticed that from what I’m trying to do overall there isn’t much of anything that works, maybe I gotta Frankenstein the code and mix and match it so it works but, what can I do? I understand AI can be a tool of sorts but personally I’m not a fan. I feel like there’s just, a key detail I’m missing but I don’t know what.

You can copy the id of the shirt on the catalog, its in the url.

replace that line with:

game.Players.PlayerAdded:Connect(function(plr)

I’m not really sure if there is another way to only allow people with a exact t-shirt other than coding because roblox doesn’t have a built in feature that you can make it so that only players with a exact clothing can use it there also isn’t much other topics or forums kind of like this.

so the error

Workspace.Script:1: attempt to index nil with ‘PlayerAdded’

means that “Players” your variable is probably nil so it’s basically trying to check to see if anything is in Players but there is nothing there because it is nothing you need to do local players = game:GetService(“Players”)
sorry didn’t read this lol

Here, I decided to write my own script and break it down step by step for you.

local Players = game:GetService("Players") -- This calls the "Players" service, that way you can acquire the "PlayerAdded" event.
local TShirtID = "http://www.roblox.com/asset/?id=12345" -- To acquire the right ID, you must go to the front page of your T-Shirt and copy the number in the URL. Then paste it over "12345" in this string

local function PlayerAdded(Player) -- When a player joins the game, this function will go through their avatar and look for the right shirt.
	if Player then
		local Character = Player.Character or Player.CharacterAdded:Wait()
		if Character then
			local TShirt = Character:WaitForChild("ShirtGraphic",8)
			if not TShirt then
				Player:Kick("You did not bring the right T-Shirt!") -- Kicks the player from the game if they are not wearing a T-Shirt.
				return
			end
			if TShirt then
				if TShirt.Graphic~=TShirtID then
					Player:Kick("You did not bring the right T-Shirt!") -- Kicks the player if they are wearing a T-Shirt, but the ID doesn't match.
				end
			end
		end
	end
end
Players.PlayerAdded:Connect(PlayerAdded) -- Connects the join event so the function executes whenever a player enters the game.

That’s it! Once you understand what the code is doing, it becomes a lot more simple.

1 Like

Thank you VERY very much, the explanations are incredibly helpful and I’ve got a better idea of how the script should work; but I notice one thing, regardless if I have the shirt or not, if I’m doing a studio test or in the game itself test, if I move it from ServerScripts to Workspace, it always kicks me, I tried making the TShirtID just a value rather then covered in quotation marks, I tried keeping it the same too, additionally I tried to check maybe it had to be my own Tshirt? Regardless, I get kicked every time under every variable I’ve tried I get kicked after about ~8 seconds. Do I need to put the script some place else? Do I need to change some variables with my whole game itself? I apologize for constantly asking for clarification but I’m genuinely super lost. Really though; thank you again for writing all of that, there are no errors (in console atleast), the only bug is just the fact you get kicked under every variable. (This is the T-shirt I’m testing out, 5645615068, its the Purple Bloxxer)

Really, thank you for your time.

No problem at all! I think a part of the reason could be that the T-Shirt isn’t loading in fast enough. The fact that it waits roughly eight seconds before kicking you is a pretty big sign.

Now, this could be a genuine error on my part, as the script expects the T-Shirt to load inside of the player’s character model in under eight seconds, which is cutting it pretty close.

To be sure, try increasing the wait time to 16 seconds. If that doesn’t help, let me know and I’ll conduct some experiments of my own to see where things went wrong.

Edit: After said experimentation, I saw it was indeed an error on my part. Change this in the PlayerAdded function:

local TShirt = Character:WaitForChild("Shirt Graphic",8)

To clarify, T-Shirts are named “Shirt Graphic” once they load into the character. I forgot that they included a space. Sorry for any inconvenience!

1 Like

Hello,

I took a moment to respond, I apologize, I was trying to work through the code to see what I could have been missing maybe, but I never managed to find out. Over this time I experimented to see what I could have been missing, I tried to put else’s after the then functions, it didn’t change a thing. I tried to see if it had to be a T-shirt I created, didn’t work either. I tried publishing the game and checking within it, but that didn’t change anything either, regardless of all the aspects I changed I got kicked. Any idea what else I could be missing? I’m really at a loss right now so any additional help would be nice. Thank you, very very much.

1 Like

Hi there!
I just made a simple script that can detect if a player is wearing a T-Shirt with the ID that you want:

--made by santos_m27

local Players = game:GetService("Players")

local TShirtID = 123
local TimeToCheck = 3
local KickMessage = "NO TSHIRT FOUND!!!"

local Connections = {}
local Tasks = {}

function CancelConnections(Player)
	if Connections[Player] then
		Connections[Player]:Disconnect()
		Connections[Player] = nil
	end

	if Tasks[Player] then
		task.cancel(Tasks[Player])
		Tasks[Player] = nil
	end
end

function Correct(TShirt)
	local Correct = false

	if TShirt then
		local IDs = {}

		for ID in string.gmatch(TShirt.Graphic, "%d+") do
			table.insert(IDs, tonumber(ID))
		end

		Correct = IDs[#IDs] == TShirtID
	end

	return Correct
end

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		CancelConnections()

		if not Player.CharacterAppearance or Player.CharacterAppearance == "" then
			Player.CharacterAppearanceLoaded:Wait()
		end

		if not Correct(Character:FindFirstChildOfClass("ShirtGraphic")) then
			Connections[Player] = Character.ChildAdded:Connect(function(Child)
				if not Child:IsA("ShirtGraphic") then return end
				if Correct(Child) then
					CancelConnections(Player)
				end
			end)

			Tasks[Player] = task.delay(TimeToCheck, function()
				Player:Kick(KickMessage)
				
				task.delay(0, function()
					CancelConnections(Player)
				end)
			end)
		end
	end)
end)

Players.PlayerRemoving:Connect(CancelConnections)

--made by santos_m27

Happy scripting!

1 Like

If you don’t mind, I think I know the issue.

You see, there is a difference between a Shirt and a T-Shirt, the main difference being how they work, they also have different ids, because they are separate items in the catalog.

If you want to use a shirt, you can use this:

local Players = game:GetService("Players")
local ID = 5768122934

Players.PlayerAdded:Connect(function(player : Player) : ()
	local character = player.Character or player.CharacterAdded:Wait()
	if not character then warn("Character not found") return end
	
	local shirt = character:FindFirstChildOfClass("Shirt")
	if not shirt then warn("Shirt not found") return end
	
	if shirt.ShirtTemplate ~= "rbxassetid://"..ID then
		player:Kick("You don't have the correct shirt")
	end
end)

Just copy the id of the shirt from the catalog, or from your own avatar, if you want to use T-Shirts, you’ll have to check for the instance ShirtGraphic, rather than the Shirt Instance, you can use this:

local Players = game:GetService("Players")
local ID = 5768122934

Players.PlayerAdded:Connect(function(player : Player) : ()
	local character = player.Character or player.CharacterAdded:Wait()
	if not character then warn("Character not found") return end
	
	local shirt = character:FindFirstChildOfClass("ShirtGraphic")
	if not shirt then warn("Shirt not found") return end
	
	if shirt.ShirtTemplate ~= "rbxassetid://"..ID then
		player:Kick("You don't have the correct shirt")
	end
end)

Replace the id with your own shirt/t-shirt, and use the appropriate script. And it should fix your issue, let me know if there’s any problems.

1 Like

I wouldn’t really call this simple for something like this, it seems like overkill honestly.

2 Likes

yea but my script tries to find a classname and not a name and it waits, thats why its like that

1 Like