Gamepasses sometimes not getting detected

My scripts don’t always detect the gamepasses and set their BoolValues to true if the players owns the gamepass.

My game adds a folder to each player that joins, named “Gamepasses”. The folder has two BoolValues in it named VIP, and DropChance. These scripts set those values to true if the player owns the gamepasses, and the VIP adds an in game chat tag. For some reason, the values of “VIP” and “DropChance” sometimes don’t get set to true, as if the script isn’t detecting the gamepass somehow, which leads me to the question of how I can fix this? Also the drop chance and vip scripts do work, but I just need the values of “VIP” and “DropChance” to actually get set to true if the player owns the correct gamepasses.

I looked for solutions, but couldn’t find exactly what I was looking for, as I’m more unsure of what the problem is with my script. I’ve already tried adding a wait on line 5, such as player:WaitForChild(“Gamepasses”) which doesn’t solve the problem.

Here are in game screen shots
https://gyazo.com/07bd3cf6e670571dbff8012896e40af1
https://gyazo.com/614d3826d5b736cb9e28e4c914b92b16
I’m the owner of the game, so I do have both gamepasses, but sometimes they just don’t get set to true.

Both of these scripts are in ServerScriptService

  • The Set VIP to True Script, ignore the code past line 5, that already works. -
local gamepassId = 17790932
local service = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
	if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then
		player.Gamepasses.VIP.Value = true
		local tags = {
			{
				TagText = "👑VIP",
				TagColor = Color3.fromRGB(255, 255, 0)
			}
		}
		local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner").ChatService)
		local speaker = nil
		while speaker == nil do
			speaker = ChatService:GetSpeaker(player.Name)
			if speaker ~= nil then break end
			wait(0.01)
		end
		speaker:SetExtraData("Tags",tags)
		speaker:SetExtraData("ChatColor",Color3.fromRGB(255, 255, 0))
	end
end)
  • The Set Drop Chance to True Script -
local gamepassId = 17790571
local service = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
	if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then
		player.Gamepasses.DropChance.Value = true
	end
end)
game.Players.PlayerAdded

sometimes this connection sets up after the first player has already joined the server.
does it only happen to the first person on the server?

good question, ill try that right now

yeah turns out that is most likely the issue, what would I use instead of game.Players.PlayerAdded?

you still want to use game.Players.PlayerAdded
but the best solution is to go over all the players already in game

make what you want to do a seperate function then at the bottem do

game.Players.PlayerAdded:Connect(Function)
for i,v in pairs(game:getservice("Players"):GetPlayers()) do
Function(v)
end

i made the name of the function you want to run for each player Function btw

this way after setting up the connection you go over the table of players already in the game

like this?

function SetGamePasses()
	local gamepassId = 17790571
	local service = game:GetService("MarketplaceService")
	game.Players.PlayerAdded:Connect(function(player)
		if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then
			player.Gamepasses.DropChance.Value = true
		end
	end)
end

game.Players.PlayerAdded:Connect(SetGamePasses)
for i,v in pairs(game:getservice("Players"):GetPlayers()) do
	SetGamePasses(v)
end

you did well, there are just some minor issues

local service = game:GetService("MarketplaceService") -- we dont need to get this service every time one time will do
local gamepassId = 17790571 -- same here, the gamepassid will be the same every time

function SetGamePasses(player)-- forgots the variable player 
	game.Players.PlayerAdded:Connect(function(player) -- ill leave this in but this line needs to be removed (you already set up the function to check if a player has been added
		if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then
			player.Gamepasses.DropChance.Value = true
		end
	end)-- this can alsow be gone
end

game.Players.PlayerAdded:Connect(SetGamePasses) -- this is good 
for i,v in pairs(game:GetService("Players"):GetPlayers()) do -- i forgot to capitilize GetService
	SetGamePasses()(v) -- needs to be SetGamePasses(v)
end
2 Likes

Just make sure to wrap your PlayerAdded like this

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function() 
        ---your code
    end)
end)

Hope this helped you out!

1 Like

i dont see him doing anything with characters tbh

Its a load check, this makes sure the player is loaded into the game. You dont need to use character but its recommended to do it like this with gamepasses

2 Likes

Still fails to set boolvalues to true sometimes when joining.

  • Updated VIP Script -
local service = game:GetService("MarketplaceService")
local gamepassId = 17790932

function SetGamePasses(player)
	if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then
		player.Gamepasses.VIP.Value = true
		local tags = {
			{
				TagText = "👑VIP",
				TagColor = Color3.fromRGB(255, 255, 0)
			}
		}
		local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner").ChatService)
		local speaker = nil
		while speaker == nil do
			speaker = ChatService:GetSpeaker(player.Name)
			if speaker ~= nil then break end
			wait(0.01)
		end
		speaker:SetExtraData("Tags",tags)
		speaker:SetExtraData("ChatColor",Color3.fromRGB(255, 255, 0))
		end
	end

game.Players.PlayerAdded:Connect(SetGamePasses)
for i,v in pairs(game:getservice("Players"):GetPlayers()) do
	SetGamePasses(v)
end
  • Updated Drop Chance Script -
local service = game:GetService("MarketplaceService")
local gamepassId = 17790571

function SetGamePasses(player)
		if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then
			player.Gamepasses.DropChance.Value = true
		end
	end

game.Players.PlayerAdded:Connect(SetGamePasses) -- this is good 
for i,v in pairs(game:getservice("Players"):GetPlayers()) do
	SetGamePasses(v)
end
local service = game:GetService("MarketplaceService")
local gamepassId = 17790571

function SetGamePasses(player)
player.CharacterAdded:Connect(function() 
		if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then
			player.Gamepasses.DropChance.Value = true
		end
	end)
end

game.Players.PlayerAdded:Connect(SetGamePasses)
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
	SetGamePasses(v)
end
1 Like

Thank you guys, helped a lot, was able to get it working!

mark OofUndefined as the solution, he came up with the check