Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make it so the game recognizes a team change?

Asked by 3 years ago

So pretty much I have a problem with the game recognizing a team change. So I made in Roblox Studio that Team red and blue are not auto-assignable but TBD is, so it automatically puts everyone who joins in TBD. And then on their Screen a frame will show with 2 image button one saying Red Team and the other saying Blue Team. They click on it and boom it changes their team to their desired color. Well the problem is the chat message saying {Team} You are now on 'Red' Team. Or whatever doesn't show up. Making the game get confused and allow them to take their own team's flag... Here is my code. So in Starter GUI I added a new ScreenGUI called TeamGUI and I made the size 1,0,1,0. I added a Frame to it. with a textbox which says Please select your Team. And 2 buttons one saying Red Team and the other saying Blue Team they are image buttons I made images on Google Draw to make it look good and added the image. Under each one I added a ObjectValue which has the Value set to it's desired team for Blue Team Button it is set to Blue team and same for red. And a local script for both buttons saying the same thing here is it's code:

local team = script.Parent.Team
local frame = script.Parent.Parent
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
    frame.Visible = false
    player.Team = team.Value
end)

So uh how do I make the game or should I say server recognize this change and give the player the message in chat?

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Look here https://devforum.roblox.com/t/player-team-detection/79645

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The server does not see the team change because it's done in a localscript. This means that other people in the server will still see the player on the TBD team because the team has only changed on the one player's screen (with the added side effect of not showing the team changed message in chat, I guess that's handled by the server). Since things done on the client are not automatically replicated to the server for security reasons (it used to, trust me it was bad), you have to use RemoteEvents and RemoteFunctions to communicate between the client and server (or, from localscripts to scripts).

To fix this: As a prerequisite, I don't know where your teams are located for them to be located in script.Parent, but they should probably be in the Teams service to avoid problems

  • create a RemoteEvent in ReplicatedStorage (or anywhere else that is replicated to BOTH the server and the client)

  • change your localscript to fire the RemoteEvent rather than changing the team directly

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

local player = game:GetService("Players").LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    -- this will tell any server scripts that are listening to this RemoteEvent to run (you can name it whatever), passing two parameters: the player who fired it (roblox gives this by default) and an extra parameter that tells the server which team the player wants to be on (change this according to what button the script is in)
    ReplicatedStorage.TeamChange:FireServer(Teams.Blue)
end)
  • create a server script that listens for the RemoteEvent:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")

ReplicatedStorage.TeamChange.OnServerEvent:Connect(function(player, team)
    -- set the team of the player that was passed into the function to the team that was passed into the function
    player.Team = team
end)

Answer this question