Gamepass Not Working

I’m working on a simulator, and basically, when you click a GUIButton, you get a click, which goes into leaderstats. I tried to edit the click script to allow me to incorporate a double clicks gamepass.

local button = script.Parent
local id = 28932538

game.Players.PlayerAdded:Connect(function(player)
	
local function onButtonActivated()	
		if game:GetService("GamePassService"):PlayerHasPass(player, id) then 
			print("Double Clicked!")
			game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Clicks").Value += 2
		else
			print("Clicked!")
			game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Clicks").Value += 1
		end
	end)
end

button.Activated:Connect(onButtonActivated)

I’m not sure what I did wrong, but a lot of stuff is underlined and clicking the button doesn’t work anymore. Any tips?

2 Likes

this should work, not sure why you had a playeradded in a local script,

add this script in the child of the button

also, make sure you have a remote event to the server including the player in the parameters to add the leaderstat value to the server so everyone can see it.

local button = script.Parent
local id = 28932538
local player = game.Players.LocalPlayer


local function onButtonActivated()
	if game:GetService("GamePassService"):PlayerHasPass(player, id) then 
		print("Double Clicked!")
		game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Clicks").Value += 2
	else
		print("Clicked!")
		game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Clicks").Value += 1
	end
end

button.Activated:Connect(onButtonActivated)
1 Like

Wait, so what do I have to do? I’m pretty new to this.

alright, so insert a local script into your button, then paste the script in.

image

reply when you did that

yeah, thats what I already have so far

okay, so test it, if you click the button check the output to see if theres any errors or what it prints.

There was no outputs in studio, but I tested it in game (thats were gamepasses work) and it said:
Game passes can only be queried by a Script running on a ROBLOX game server

im not sure why, can you screenshot your placement of the script and the contents of the script, please?

Use MarketplaceService, GamePassService only works for old gamepasses.

forgot about that, my bad. twentytwocharacters

After typing that, it says: PlayerHasPass is not a valid member of MarketPlaceService “MarketPlaceService”

MarketplaceService uses different functions:
(heres code that will work)

--//Services
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Button = script.Parent

--//Controls
local GamepassID = 28932538

--//Functions
Button.MouseButton1Click:Connect(function()
	if MarketplaceService:UserOwnsGamePassAsync(LocalPlayer.UserId, GamepassID) then
		print("Double Clicked!")
		LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Clicks").Value += 2
	else
		print("Clicked!")
		LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Clicks").Value += 1
	end
end)

Two problems:

  • Clicks don’t appear on the leaderboard for others (may be the leaderboard script, not sure)
  • Even as an owner of the game, I’m still not getting double clicks. It just adds 1 everytime.

This is because the script is local. You would need to use remote events for this to be showable on all leaderboards for other people then the local player.

Doing this method could also make the system more secure.

How exactly would I do this? Like I said, I’m pretty new to this and I’m not very skilled in scripting.

Just coding up an example. Insert a remote event into replicated storage and call it “GamepassRemote” without the quotation.

I created the remote event in ReplicatedStorage

Can you send a copy of your leaderstats script so I can see what you have called your values please. Almost done.

local players = game:GetService('Players')

players.PlayerAdded:Connect(function(player)
	if player then
		local folder = Instance.new('Folder')
		folder.Name = 'leaderstats'
		folder.Parent = player
		local clicks = Instance.new('IntValue')
		clicks.Name = 'Clicks'
		clicks.Parent = folder
		clicks.Value = 0
		local coins = Instance.new('IntValue')
		coins.Name = 'Coins'
		coins.Parent = folder
		coins.Value = 50
	end
end)

Funnily enough, I didn’t write the entire thing. Me and a friend hired someone to script some stuff, but he quit halfway through. I had to revamp all his stuff

Alright you need to insert a script into ServerScriptService and insert this code:

local MarketPlaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("GamepassRemote")

local GamepassID = 28932538

local function Add(player)
	print("Received Remote")
	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
		local Leaderstats = player:WaitForChild("leaderstats"):WaitForChild("Clicks")
		Leaderstats.Value = Leaderstats.Value + 2
	else
		local Leaderstats = player:WaitForChild("leaderstats"):WaitForChild("Clicks")
		Leaderstats.Value = Leaderstats.Value + 1
	end
end

Remote.OnServerEvent:Connect(Add)

In the local script insert this code: (In the gui button)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("GamepassRemote")

script.Parent.MouseButton1Down:Connect(function()
	print("Sent Remote")
	Remote:FireServer()	
end)

The benefits of checking if a user owns the gamepass on the server is the fact that its not exploitable as any local script a client would have access to but server scripts they don’t.