I’ve got a gui button with a local script that when pressed triggers a RemoteEvent containing the team name the player wants to change to and the players name, it goes to a script in ServerScriptService which is supposed to change the player to that team, everything works just fine except for the one line of code that is actually supposed to change the player to that team, could someone point out what I’m supposed to put in?
The local script in the GUI:
local Button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("ChangeTeam")
Button.MouseEnter:Connect(function()
print("Entered")
Button.ImageColor3 = Color3.new(255, 255, 255)
end)
Button.MouseLeave:Connect(function()
print("Left")
Button.ImageColor3 = Color3.new(0.72549, 0.72549, 0.72549)
end)
local function onButtonActivated()
print("Button activated!")
RemoteEvent:FireServer(Button)
end
Button.Activated:Connect(onButtonActivated)
The server script to change the team after receiving the RemoteEvent:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:FindFirstChild("ChangeTeam")
local Teams = game:GetService("Teams")
local function Change(player, Button)
print(Button)
print(player)
player.Team = -- Not sure what is supposed to go here to change the player to the required team
end
RemoteEvent.OnServerEvent:Connect(Change)
Use an if statement to find the player’s team and then change it.
Sample code
if player and player.TeamColor == BrickColor.new("Bright red") then
--Change the team
end
https://developer.roblox.com/en-us/api-reference/class/Team
The server script is getting the team the player wants to join through the RemoteEvent, that’s what the “Button” is, I can’t retrieve the team from game.Teams for some reason, “Button” holds the correct name but it fails to use this to fetch the team.
player.Team = game.Teams.Blue --Change to the name of the team
This should help you change to the required team
Yes but I can’t use the name of the team directly, because it has to vary depending on what team the player chooses, the RemoteEvent holds that required team as “Button”.
If I tell it “print(Button)” it prints the name of the team I want, but it won’t allow me to use the name from button, it can’t find the team.
Wait, so the button is the name of the team?
If that’s the case then
player.Team = game.Teams:FindFirstChild(change)
Okay, so I tested it and it works. Here are my sample scripts
ButtonScript
local player = game.Players.LocalPlayer
local rm = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local bot = script.Parent
bot.MouseButton1Click:Connect(function()
rm:FireServer(bot)
end)
ServerScript
local rm = game.ReplicatedStorage:WaitForChild("RemoteEvent")
rm.OnServerEvent:Connect(function(player, change)
print(change.Name)
if game.Teams:FindFirstChild(change.Name) then
print(1)
player.Team = game.Teams:FindFirstChild(change.Name)
end
end)
Ahh yes this is perfect, thankyou so much!