Hello devforum members! How can I make a button only work if the player clicking is from a specific team in Roblox Studio? For example, if the player team = TeamBlue and clicks on a button, the trigger print(“banana”) will occur. And if the player who clicked on the button is not from team = TeamBlue, nothing will happen.
You can use the player’s Team property.
local BlueTeam = game:GetService("Teams").TeamBlue -- However you reference the team
local Player -- However you reference the player
if Player.Team == BlueTeam then
print("Banana")
end
local Player must be the player who clicked the button, how do I do this?
Well it depends, I don’t know what type of instance your button is. If your button is operated using a
ClickDetector, you would use ClickDetector.MouseClick, which returns the player that clicked on the part:
local BlueTeam = game:GetService("Teams").TeamBlue -- However you reference blue team
local Button -- However you reference the button
local ClickDetector = Button.ClickDetector -- However you reference your clickdetector
ClickDetector.MouseClick:Connect(function(Player)
if Player.Team == BlueTeam then
print("Banana")
end
end)
Case1: If your button is a GuiButton, I assume that you’re detecting the click using a localscript. If that is the case, you can just reference the player like so:
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local BlueTeam = game.Teams.TeamBlue
local Button = PlayerGui.Button -- However you reference the button
Button.MouseButton1Click:Connect(function()
if Player.Team == BlueTeam then
print("Banana")
end
end)
Case 2: If your button is a GuiButton and you needed to check their team on the server, you would use a RemoteEvent to do so. OnServerEvent returns the player that fired the RemoteEvent as the first parameter:
-- Localscript
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local BlueTeam = game.Teams.TeamBlue
local Button = PlayerGui.Button -- However you reference the button
local RemoteEvent -- However you reference the RemoteEvent
Button.MouseButton1Click:Connect(function()
RemoteEvent:FireServer()
end)
-- Server script
local RemoteEvent -- However you reference the RemoteEvent
local BlueTeam = game.Teams.TeamBlue
RemoteEvent.OnServerEvent:Connect(function(Player)
if Player.Team == BlueTeam then
print("Banana")
end
end)
My button uses a ClickDetector. Thanks for showing how to make this work and for showing other methods of how to do this, like the gui one that maybe I can use at another time.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.