Team Change Command w/ Cmdr

[STILL ACTIVE]

I’ve made a team change command with Cmdr. I need help on the Server side of the command.
Here is what I have so far:

-- teamchangeServer

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

return function(player, team)
	local team = team.Name
	if team then
		player.Team = team
		
	end
	return("Changed Team.")
end

And here is the other script

return {
	Name = "changeteam",
	Aliases = {"teamchange", "ct", "tc"},
	Description = "Changes the person who is using the command's team.",
	Group = "Developer",
	Args = {
		{
			Type = "team",
			Name = "Team",
			Description = "The team you want to switch to"
		},
		
	}
}

image

I am just confused on how I would make the server script because it is not actually changing the team but the console is saying that team was changed. Any help would be much appreciated.

2 Likes

You could maybe try debugging it first and using print(team) to check if the team is even correct

2 Likes

So I did that and heres Cmdr.
image
And the output
image
So it’s printing the team name but it’s not changing the player’s team?

Ok so I’ve tried a using local player and this is what it says.
image

Here is the code:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local plr = Players.LocalPlayer

return function(player, team)
	local team = team.Name
		plr.Team = team

	return("Changed to team "..team..".")
end

You are taking the Player in as “player,” and trying to run “plr.Team.” Try changing line 7 to:

player.Team = team
2 Likes

This still does not work. Nothing in output.

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

return function(player, team)
	local actualTeam = Teams[team]
	if actualTeam then
		player.Team = actualTeam
		return("Changed Team.")
	end
    return("No team with name "..team)
end

Pretty sure it’s just a naming problem.

Just tried,


image

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

return function(player, team)
	local actualTeam = Teams[tostring(team.Name)]
	if actualTeam then
		player.Team = actualTeam
		return("Changed Team.")
	end
    return("No team with name "..team)
end

Perhaps this’ll work?

Now it’s just saying, “Changed Team.” Though, I don’t understand because It’s not a string that I’m typing in. I’m selecting it from Cmdr’s types called Team.

The player variable in there is the context so what I would try to do is probably player.Executor.Team and then try setting the team to whatever team the team variable returned.

Now I get an error that says:

Unable to assign property Team. Object expected, got string

Here’s the code:

return function(context, team)
	local plrteam = team.Name
	if team then
		context.Executor.Team = plrteam

	end
	return("Changed Team.")
end

I think you’re getting an error because the plrteam variable is the name of the team argument. I would probably just remove the team.Name and see if it works.

Errored again:

Expected Team got Player for Player:Team.

Code:

return function(context, team)
	--local plrteam 
	if team then
		context.Executor.Team = team

	end
	return("Changed Team.")
end

Okay, I think this is the solution you’ve been looking for.

Now, if we look at this line:


return function(player, team)
	local team = team.Name
	if team then
		player.Team = team --you're trying to set an object to a string
		
	end
	return("Changed Team.")
end

It looks like you’re setting an object, our team, to a string value. This is not what you want.

Instead, you should do this:


return function(player, team)
	local team = Teams:FindFirstChild(team.Name)
	if team then
		player.Team = team
		
	end
	return("Changed Team.")
end

This edited bit up here, instead, goes into the Teams instance and looks for the team that you want.

Hope it helps!

So now it just returns, “Changed Team” but the team doesn’t actually change.

Try printing the variable team. Is it assigned to an instance?

Also, I forgot.
You should probably change this:

	if team then
		player.Team = team
		
	end
	return("Changed Team.")

…to this:

	if team then
		player.Team = team
		return("Changed team.")
	else
		return("Could not change team.")
	end

It printed as nil
image

Does your team exist as the EXACT name in your Teams instance?

If not, this is why you might be getting a nil instead.

image
Yes I’m using the team function provided by Cmdr