Why won't it set a player to a team if they own a gamepass?

I am making it so if you own a gamepass and you click a button, you can go on a special team, but it is not working with no errors.
Script:

function Click(mouse)
local gamepassID = 9597486
if script.Parent.Parent.Parent.Parent.Parent:IsInGroup(6068826) and script.Parent.Parent.Parent.Parent.Parent:UserOwnsGamePassAsync(userID, gamepassID) then 
script.Parent.Parent.Parent.Parent.Parent.TeamColor = BrickColor.new("Daisy orange") 
end 
end 

script.Parent.MouseButton1Click:connect(Click) 
2 Likes

First all of those parents are very unnecessary, try defining a variable as player and use game.Players.LocalPlayer since this is a local script.

Second please use Connect not connect for it is deprecated.

The mouse parameter is also unnecessary

2 Likes

I’ve done

function Click(mouse)
local Player = game.Players.LocalPlayer
local gamepassID = 9597486
if Player:IsInGroup(6068826) and Player:UserOwnsGamePassAsync(Player.UserId, gamepassID) then 
Player.TeamColor = BrickColor.new("Daisy orange") 
end 
end 

script.Parent.MouseButton1Click:Connect(Click) 

and still wont work

1 Like

I’ve been away from Roblox until recently due to college, so I’m refreshing my brain.
Your use of a LocalScript may be the issue. This is because the tasks you are doing (such as checking for an asset) are taken care of on Roblox’s servers, so it may not work in a LocalScript, though I have not tried. Try this code inside a regular Script (which is a server script).

game.Players.PlayerAdded:Connect(function(Player)
--local Player = game.Players.LocalPlayer   <<< can't use this in a server script
   local gamepassID = 9597486
   if Player:IsInGroup(6068826) and Player:UserOwnsGamePassAsync(Player.UserId, gamepassID) then 
       Player.TeamColor = BrickColor.new("Daisy orange") 
   end  
end)


2 Likes

Can I make it

function Click(mouse)

game.Players.PlayerAdded:Connect(function(Player)
   local gamepassID = 9597486
   if Player:IsInGroup(6068826) and Player:UserOwnsGamePassAsync(Player.UserId, gamepassID) then 
       Player.TeamColor = BrickColor.new("Daisy orange") 
   end  
end)
end 
script.Parent.MouseButton1Click:Connect(Click) 

so that it does that when the gui button is clicked?

Yes, I just wanted to see if my code would fix it. Did it work?

To make the click work, use a remote event. Look at the Roblox webpage about it if you are not familiar with them. With the remove event, it can be arranged so that when the player clicks the button, the client sends a request to the server, and the server does whatever it needs to do.

1 Like

there is no way for me to check since I have to get the gamepass but I don’t have robux

It would be insecure if the client side had the authority to put you in teams.
This is why you need to use a remote event or remote function.

Something like this would do what you’re looking for

-- LocalScript (inside the button Ui)
 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local requestTeamChangeEvent = ReplicatedStorage:WaitForChild("requestTeamChangeEvent")

-- When you press the button, fire that specific event
function Click(mouse)
  requestTeamChangeEvent:FireServer()
end 

script.Parent.MouseButton1Click:connect(Click)
-- Script (inside ServerScriptService)

local gamepassID = 9597486
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local requestTeamChangeEvent = Instance.new("RemoteEvent", ReplicatedStorage)
requestTeamChangeEvent.Name = "requestTeamChangeEvent"
 
-- When this specific event is fired, switch the player's team
local function onCreatePartFired(player)
	print(player.Name, "wants to switch teams!")
  if player:IsInGroup(6068826) and player:UserOwnsGamePassAsync(userID, gamepassID) then 
    player.TeamColor = BrickColor.new("Daisy orange") 
  end
end
 
requestTeamChangeEvent.OnServerEvent:Connect(onCreatePartFired)

You can read more about the server client model here

1 Like

It doesn’t work with no errors at all