Error with Remote Events

I am trying to make a script that changes the team of the player when they click a button but I keep getting this error and I don’t know how to fix it.

-- Local Script
user = game.Players.LocalPlayer
team = "Civilian"
script.Parent.MouseButton1Click:Connect(function()
		game.ReplicatedStorage.ChangeTeam:FireServer(team, user)
	script.Parent.Parent.MP.Text = "Success!"
	wait(1)
	script.Parent.Parent.MP.Text = "Civilian"
end)

-- Server Script
game.ReplicatedStorage.ChangeTeam.OnServerEvent:Connect(function(team, player)
	print(team)
	player.Team = game.Teams[team]
end)
1 Like

.OnServerEvent connections always receive the instance of the player who sent the remote event as its first parameter. That means you don’t need to fire game.Players.LocalPlayer instance yourself, simply fire “team,” and have the server event’s parameters set to player, team as player will always be the first parameter listed.

1 Like
-- Local Script
user = game.Players.LocalPlayer
team = "Civilian"
script.Parent.MouseButton1Click:Connect(function()
		game.ReplicatedStorage.ChangeTeam:FireServer(team)
	script.Parent.Parent.MP.Text = "Success!"
	wait(1)
	script.Parent.Parent.MP.Text = "Civilian"
end)
-- Server Script
game.ReplicatedStorage.ChangeTeam.OnServerEvent:Connect(function(player, team)
	print(team)
	player.Team = game.Teams[team]
end)

Now it just says that it is null

Running the same I don’t receive that issue:

  Civilian --correct printout on serverside
  Civilian is not a valid member of Teams "Teams" -- I don't have teams setup

Is ChangeTeam a proper RemoteEvent in ReplicatedStorage? Not a BindableEvent.

It says its a remote event I have also tried changing the input but still nothing

Anywhere else in your localscript that may be changing what team equals? Your exact snippet worked for me so it may be a problem exterior to it. Is team also declared as a local variable somewhere above? i.e. local team = nil

The only thing I could think of is that I have like 4 other scripts that allow for team change with the same script (with checking if user is in a group)

Ideally you should consolidate all your code that has a similar purpose into one source. Is it necessary for many files to do the same thing?

Since the team object is a team, just do this, also flip player and team, they’re the wrong way around

game.ReplicatedStorage.ChangeTeam.OnServerEvent:Connect(function(player, team)
	print(team)
	player.Team = team
end)

And RemoteEvent:FireServer does not take a player argument, this is automatically inferred, you should change that line to

game.ReplicatedStorage.ChangeTeam:FireServer(team)

Whats actually happening is you’re tyring to get a child of an Instance with an Instance (which isn’t allowed), Instance[k] will only work if k is a string, and since you’re sending the Team object directly without getting it’s Name, it throws this error.

Just assign it directly.

1 Like

I just changed the variables to be local and now it prints the name but does not change the team and gives same error

thanks for the help @metatablecatmaid and @MP3Face