I need help with this, I don't know much about GUI's.
How do I identify a player that clicks a GUI button and how to change teams with the button?
3 answers
The client automatically tells you when a player has clicked a button. You just need a script linked to a function to do something when they click that button.
For example, if you had a LocalScript in a PlayerGui inside the StarterGui, you'd need this in it after putting a button in the StarterGui on the screen.
local button = script.Parent:WaitForChild('Button') --Make sure the button name is 'Button' or this will infinitely yield.
button.MouseButton1Click:Connect(function()
--Invoke a ChangeTeam remote/event here
end
To change a players team you'd probably need to be communicating with the server. So you'd need to learn about RemoteEvents and RemoteFunctions first.
Hope this helped.
Building off what the previous answer said, you can detect when a button is pressed VIA:
-- LocalScript put under the TextButton.
local button = script.Parent;
button.MouseButton1Click:Connect(function()
RemoteEvent:FireServer();
end)
-- ServerScript
RemoteEvent.OnServerEvent:Connect(function(player)
-- Set the player's team here.
end)
If you are using a LocalScript inside of the button you can do it this way:
Put a RemoteEvent in ReplicatedStorage
Put a ServerScript in ServerScriptService
Put a LocalScript inside the button
Code:
LocalScript
local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") --// Change RemoteEvent to the name of the event and the location
script.Parent.MouseButton1Click:Connect(function()
local teamName = "Civillian" --// Change this to the team you want them to be switched to
event:FireServer(teamName)
end)
Here, we are getting the RemoteEvent, and once the button is pressed it fires the server with 2 arguments, the player (automatically the first argument) and the Team Name.
ServerScript
local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") --// Change RemoteEvent to the name of the event and the location
event.OnServerEvent:Connect(function(player,teamName)
local character = player.Character or player.CharacterAdded:Wait()
if character then
player.TeamColor = game:GetService("Teams"):FindFirstChild(teamName).TeamColor
wait(0.25)
player:LoadCharacter()
end
end)
Here we are getting the RemoteEvent, and when the server is fired and it connects the arguments of the player and the team name, it will check if the player has a character, and if it does then it will change their team and respawn them.
Now there's a different way, you can use a ServerScript inside the button, like this.
ServerScript
script.Parent.MouseButton1Click:Connect(function(player)
local player = script.Parent.Parent.Parent.Parent --// Change this to parent the game (this will act as the player)
local character = player.Character or player.CharacterAdded:Wait()
if character then
player.TeamColor = game:GetService("Teams"):FindFirstChild("Enter Team Name Here").TeamColor --// Change "Enter Team Name Here" To the team you want them to switch to in Teams
wait(0.25)
player:LoadCharacter()
end
end)
Here, we are getting the player through PlayerGui, a child of the Player. After that, we're checking if the player has a character, if so then we will change their team and respawn them.
My opinion in this would be the first one using RemoteEvents, just because it's less of a hassle.
Hope this helped!
If this is what you're looking for, don't forget to select this as the answer to your question!
~killerbrenden
This discussion is preserved as it was. New answers, comments, and votes are closed.
