Button not reacting correctly

  1. What do you want to achieve? team changer that changes your team if you got the gamepass.

  2. What is the issue? Not reacting working even if you don’t have the gamepass

  3. What solutions have you tried so far? Haven’t found yet.

  • The client that connected to the button
local id = 21404026

game.Players.PlayerAdded:Connect(function(Player)
		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, id) then
			script.Parent.MouseButton1Click:Connect(function()
				game.ReplicatedStorage.TeamChanger:FireServer(script.Parent.D.Text)
				print("..Player ..")
			end)
		end
	end)
  • The server event script
local TC = Instance.new("RemoteEvent")
TC.Name = "TeamChanger"
TC.Parent = game.ReplicatedStorage

TC.OnServerEvent:Connect(function(Player, TeamName)
	Player.Team = game.Teams[TeamName]
	Player:LoadCharacter()
end)

Use game:GetService("Players").LocalPlayer in local scripts instead of PlayerAdded event. And use game:GetService("Teams"):FindFirstChild(TeamName)

And how am I able to connect it into a function with local player??

local id = 21404026

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

if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, id) then
	script.Parent.MouseButton1Click:Connect(function()
			game.ReplicatedStorage.TeamChanger:FireServer(script.Parent.D.Text)
			print("..Player ..")
	end)
end

You won’t even need the player added function!

I’ve tried it’s sadly still teaming people even though they don’t have the gamepass

Do you have any idea why it’s not working??

If im not mistaken, You shouldn’t check if the player owns a gamepass inside a local script, as it makes it easily exploitable, do this check on the server script instead

You cannot directly get the service

Your Script:

local id = 21404026

game.Players.PlayerAdded:Connect(function(Player)
		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, id) then
			script.Parent.MouseButton1Click:Connect(function()
				game.ReplicatedStorage.TeamChanger:FireServer(script.Parent.D.Text)
				print("..Player ..")
			end)
		end
	end)

Working script:

local id = 0000

script.Parent.MouseButton1Click:Connect(function()
   if game.MarketPlaceService:UserOwnsGamepassAsync(game.Players.LocalPlayer.UserId,id) == true then
     game.ReplicatedStorage:WaitForChild("TeamChanger"):FireServer(script.Parent.D.Text)
 end
end)

You cannot get game.Players.PlayerAdded from a local script, second you cannot tell player, you can only tell local player which means client from a local script

You can use game.Players.PlayerAdded on a local script, tough you wont get a response from your own character since by the time the local script executes, your player will have already loaded in the server.

Just here to fix something, .PlayerAdded isn’t a service, it’s an event.

Yes, also I told marketplace service not players added service and its better to get a variable for all types of it instead of loops