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

Why is my officer class selecting script not working?

So, the script provided below is about a remote event being fired into a server script (not sure if I used the right words but you get the idea) and so that once the remote event is fired, this script will run. The output doesn't show any errors but it's still not working.

So in the script, after checking if the player is inside the "France" team, then the script should kill the player so they can reset and be given proper uniform for their class (officer). And then after that, I used the CharacterAdded event to detect when they re spawn and give them their proper uniform. But it won't work. I think this is where the issue is, but I can't find it.


-- SERVICES
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game:GetService("Players")
local Team = game:GetService("Teams")

--EVENTS
local OfficerEvent = ReplicatedStorage:WaitForChild("OfficerEvent")
local InfantryEvent = ReplicatedStorage:WaitForChild("InfantryEvent")
local RiflesEvent = ReplicatedStorage:WaitForChild("RiflesEvent")



-- OFFICER EVENT

OfficerEvent.OnServerEvent:Connect(function(player)
	local character = player.Character
		if player.Team == game.Teams.France then
			player.Character.Humanoid.Health = 0
			player.CharacterAdded:Connect(function()
		character:WaitForChild("Shirt").ShirtTemplate = "rbxassetid://1422626514"
		character:WaitForChild("Pants").PantsTemplate = "rbxassetid://1070344465"
		end)
	end
end)

Thanks in advance!

2 comments

0

I assume that the CharacterAdded event isn't firing, because it only has the chance to fire when OnServerEvent fires. So what you can do is, call the :FireClient() inside the CharacterAdded. So basically put the CharacterAdded in the localscript instead.

0

But I want the ChararcterAdded and the code below it to be seen by other players as well

1 answer

That's a common mistake there. You defined a player's team as an actual team, and that won't work.

if player.Team == game.Teams.France then won't work, because while this may seem logical, it is not to the game.

Here is a proper way to define this: if player.TeamColor == game.Teams.France.TeamColor then

Aditionally, often times you may encounter that people are not wearing clothing, such as pants or a shirt at all, here is how we can prevent bugs about it and solve it:

function changeShirt(character,shirtTemplateId) -- This function takes 2 parameters, the Character of the player and the ShirtTemplate ("rbxassetid://XXXXXX")
	local hasShirt = false
	local shirt = nil
	for i,v in pairs(character:GetChildren()) do
		if v:IsA("Shirt") then
			hasShirt = true
			shirt = v -- If there is a shirt existing, then we will define the valuable "shirt" as it, so we can use it later.
		end
	end
	if hasShirt == false then -- It is important to mention that this will run AFTER the loop, so no worries!
		shirt = Instance.new("Shirt",character)
		shirt.ShirtTemplate = shirtTemplateId
	else
		shirt.ShirtTemplate = shirtTemplateId
	end
end

function changePants(character,pantsTemplateId) -- This function takes 2 parameters, the Character of the player and the PantsTemplate ("rbxassetid://XXXXXX")
	local hasPants = false
	local pants = nil
	for i,v in pairs(character:GetChildren()) do
		if v:IsA("Pants") then
			hasPants = true
			pants = v -- If the person has pants, we save 'em into the pants variable.
		end
	end
	if hasPants == false then -- Again, this runs after the loop is done.
		pants = Instance.new("Pants",character)
		pants.PantsTemplate = pantsTemplateId
	else
		pants.Pants = pantsTemplateId
	end
end

We call it using:

player.CharacterAdded:Connect(function(character)
	changeShirt(character,"rbxassetid://1422626514")
	changePants(character,"rbxassetid://1070344465")
end)

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