Help with changing a players team

I am trying to have a gui panel where when you click a button it changes the players team but it is giving me this error:
17:15:09.434 - Players.henberrysodapop.PlayerGui.Teams.Frame.TextButton.Script:7: attempt to index nil with ‘Team’
I used a local script but that did not work because then others could not see that you have changed teams, here is my current script:

local Button = script.Parent


Button.MouseButton1Click:Connect(function(plr)
	local Ran = math.random(1,2)

	if Ran == 1 and plr.Team ~= game.Teams.Judges and game.Workspace.GlobalValues.Judges.Value <= 3 and plr.leaderstats.Talent.Value >= 10 then
		game.Workspace.GlobalValues.Judges.Value = game.Workspace.GlobalValues.Judges.Value + 1
		plr.Team = game.Teams.Judges
		plr.leaderstats.Talent.Value = plr.leaderstats.Talent.Value - 10
		plr.Character:MoveTo(Vector3.new(106.64, 257.925, 234.14))
	elseif Ran == 2 and plr.Team ~= game.Teams.Judges and game.Workspace.GlobalValues.Judges.Value <= 3 and plr.leaderstats.Talent.Value >= 10 then
		game.Workspace.GlobalValues.Judges.Value = game.Workspace.GlobalValues.Judges.Value + 1
		plr.Team = game.Teams.Judges
		plr.leaderstats.Talent.Value = plr.leaderstats.Talent.Value - 10
		plr.Character:MoveTo(Vector3.new(106.6, 257.875, 52.71))
	end

end)

thank you!

player is not a parameter of MouseButton1Click
also is this in a Local Script or Server Script?
Server Scripts cannot fire Mouse Events

This is a server script, should I use activated instead, I know that works.

You would have to use the local script to fire a RemoteEvent or RemoteFunction and pick up the info with remoteevent.OnServerEvent or like remotefunction.OnServerInvoke

So i would just put a remote event in the local script and call it from a server script?
Also, happy birthday!

If the script under the gui button is not a local script, change it to a local script. Insert a remote event into replicated storage and insert a script into ServerScriptService.

-- Local script 
script.Parent.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.RemoteEventName:FireServer()
end)

--Server Script 
game.ReplicatedStorage.RemoteEventName.OnServerEvent:Connect(function(plr) 
    -- Insert your randomiser and team change code here
end) 
1 Like

You can not get the player who clicked via server script if it is in a text button. You can only do this in a ClickDetector, players.PlayerAdded/ChildAdded, or in a Touch event. The best you can do is use a local script which LIKELY will not work as it will replicate to the client and not the server! So the best you can do is use remote events to send data/arguments through a local script to a server script with remote:FireServer() and OnServerEvent. You can learn how to do this here:

The error is telling you so much and you’re ignoring it!

17:15:09.434 - Players.henberrysodapop.PlayerGui.Teams.Frame.TextButton.Script:7: attempt to index nil with ‘Team’

Let’s break it down.

Players.henberrysodapop.PlayerGui.Teams.Frame.TextButton.Script

The error was in the script in this location. You already know that.

Script:7:

The error was on the 7th line, right here:

if Ran == 1 and plr.Team ~= game.Teams.Judges and game.Workspace.GlobalValues.Judges.Value <= 3 and plr.leaderstats.Talent.Value >= 10 then

attempt to index nil with ‘Team’

Indexing something in Lua is trying to access a table or object’s properties. Like script.Parent, in which you index Script with Parent.
The error is telling you that you’re trying to get nil.Team.
On that line, there is plr.Team. This is what is causing the error.
plr is nil.

Button.MouseButton1Click:Connect(function(plr) -- there is no plr!

MouseButton1Click is not supposed to give anything to the function.

The fix is to find the player in some other way.
In a LocalScript, you can write

-- LocalScript
local plr = game.Players.LocalPlayer

-- Script
local plr = script.Parent.Parent.Parent.Parent.Parent.Parent

(The Script example only works as-is if the Script is in the GUI)

2 Likes

You have to fire a remote event to the Server and change the team from a normal script by calling the event.

1 Like