LocalScript vs. Script help

So I’ve been having a problem with my game which is when a player clicks the GUI to change teams, it only changes their team for them. Therefore, everyone else is on the “choosing” team besides them, and it’s the same for everyone. I realized it was because my team changer was in a LocalScript instead of a Script. I tried just copying and pasting the code, however, scripts do not use the “local Player = game.Players.LocalPlayer” code. Is there a way I can substitute that line of code for one that works in a normal script?

7 Likes

Local script

A local script is client sided and will only happen to the player

Script

A script is server-sided and will happen to all the players

6 Likes

Are you saying it SHOULD be a local script, otherwise it will change the team of all players?

1 Like

Scripts do things on the server. If you change a player’s team using a script, it will actually change the player’s team. If you change the player’s team on a LocalScript, it will only change that team for them, meaning that it won’t change the team (for everyone else and the server) as the server won’t be able to see it.

2 Likes

You need a LocalScript to handle the user interface (ie; displaying the GUI and handling the logic when the player clicks). You also need a server Script in order to actually change the players team property, as only the server may do this in a manner that replicates to other clients. To connect the LocalScript and ServerScript, you need to use a RemoteEvent or RemoteFunction.

2 Likes

When working with a GUI, you always work a local script because the clients computer is where the GUI is being held at and not the server. So to communicate to the server from the client you are gonna have to use a RemoteEvent. In the GUI, when a player clicks a button for example such as “Red Team” you could then send parameters to the RemoteEvent which could be placed in ReplicatedStorage.

Here is a code example for this in the local script of the client GUI:

local player = game.Players.LocalPlayer
local remoteevent = game.ReplicatedStorage.RemoteEvent

RedTeamButton.MouseButton1Click:Connect(function()
remoteevent:FireServer("RedTeam")
end)

BlueTeamButton.MouseButton1Click:Connect(function()
remoteevent:FireServer("BlueTeam")
end)

Here is an example of how you would handle it on the Server, clients automatically send an argument of the player, the 2nd argument we sent from the client is what team they chose.
Here is an example of how you can handle this from a Server Script:

local Teams = game:GetService("Teams")
local remoteevent = game.ReplicatedStorage.RemoteEvent

remoteevent.OnServerEvent:Connect(function(player, teamcolor)
	if teamcolor == ("RedTeam") then
		player.Team = Teams.RedTeam
	elseif teamcolor == ("BlueTeam") then
		player.Team = Teams.BlueTeam
	end
end)
9 Likes

Would I put said Server Script in the ServerScriptService?

1 Like

Pretty sure you would.
Generally you can place them wherever, as long as you have them organised.

I’ve even seen scripts in Lighting, but not sure if you should do that or if it’s an old practise.

2 Likes

You generally want Server Scripts in ServerScriptService or in Workspace. ServerScriptService is held at Roblox’s Servers in which it handles what happens to the server, which then replicates to all clients. Lighting used to be used as a storage unit back in the days, but now there is ServerStorage! :slightly_smiling_face:

4 Likes

I believe I did everything right according to my game but it’s not working.
This is the Script in the ServerScriptService:
image
I have a Remote Event in the ReplicatedStorage named “TeamEvent”
This is the LocalScript in inside of the StarterGUI scripts folder I have:

You are firing the wrong arguments, you have to fire the following for the following buttons:

For Republic button:
remoteevent:FireServer("Republic")
For Raiders button:
remoteevent:FireServer("Raiders")
For Civillians:
remoteevent:FireServer("Civillians")

1 Like

Do I have to have Remote Events named each of those? Or is that for the buttons?

No, you are firing the arguments to the same remote event. I noticed as well an error in your local script where you are referring to your buttons from the StarterGUI and not the actual GUI of the player. You must have the local script inside the GUI and refer to the buttons as for example:

local Raiders = script.Parent.TeamSelect.Hostiles

1 Like

You’re a god. Thank you it works now. I’m learning so much doing this.

1 Like

I have a new problem now. I have the team selector set to a group only script so only the group members can choose the republic team, and if they’re in the group they cannot choose the hostile or civilian team. However with this new one, it will let anyone pick the republic team. How can I get around this?

Check that the player is in the required group after firing the RemoteEvent but before changing their team.

game.Players.LocalPlayer can only be called from a localscript because the localplayer is the player who ran the program in the first place. It can’t be called from a regular script.
However, there are several ways to get past this.
My reccommended methos (based on your situation) is touse FireServer() and fire it from the localscript.
It might look something like this:

Localscript in the button:

script.Parent.MouseButton1Click:Connect(function()
    local plr = game.Players.LocalPlayer
game.ReplicatedStorage.RemoteEvent:FireServer(plr) -- Change the name of RemoteEvent to whatever your RemoteEvent is called (must be in replicated storage)
end)

This is the script that would be in ServerScriptService:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(_,plr) -- again, change RemoteEvent to the name of your RemoteEvent in ReplicatedStorage
plr.Team = game.Teams.Team -- name of your team
end)

For multiple teams you will have to send some extra values through the server as well as the player, or you can have multiple RemoteEvents.

Hope this helped!
~ salcret

4 Likes

Thank you this helped me! I.BlueTeam have a better understanding now

Wow well put and thanks this helps me a lot with the server to client methods