Problem with Touch:Connect()

Hello Developers,

So I have been working on a simple “system” where if you touch a block, you will be given one pop. If you own the V.I.P gamepass, you get two. The problem with this is when ever a player with the gamepass joins and starts to touch the blocks, the rest of the server gets two pops, below is the code.

--// Variables

local PopIt = script.Parent
local Using = false
local M_ = require(game.ReplicatedStorage.Information.Settings)
local Pops = M_["Pops"]
local Cooldown = M_["CooldownForBig"]
local Market = game:GetService("MarketplaceService")
local Gamepass = M_["Vip"]
game.Players.PlayerAdded:Connect(function(Player)
	
local function Pop(hit)
	local human = hit.Parent:FindFirstChildWhichIsA("Humanoid")
		if human and Using == false then
			local player = game.Players:FindFirstChild(hit.Parent.Name)
			Using = true
			PopIt.CanCollide = false
			PopIt.Transparency = 1
			player.leaderstats.Pops.Value = player.leaderstats.Pops.Value + Pops
		
			if Market:UserOwnsGamePassAsync(Player.UserId, Gamepass) then
				player.leaderstats.Pops.Value = player.leaderstats.Pops.Value + Pops
			end
		end
		
		wait(Cooldown)
		PopIt.CanCollide = true
		PopIt.Transparency = 0
		Using = false
		
	end
script.Parent.Touched:Connect(Pop)
	
end)

I appreciate any certain type of help with this problem.

Is the scope of Pops available to anyone? If so then everyone is getting the same Pops value. To fix this make sure that the value of Pops is 1 when a normal player gets it, and change it to 2 when someone with VIP gets it.

if Market:UserOwnsGamePassAsync(Player.UserId, Gamepass) then
	player.leaderstats.Pops.Value = player.leaderstats.Pops.Value + 2
else
   	player.leaderstats.Pops.Value = player.leaderstats.Pops.Value + 1
end

or

local Pops, vip_Pops = 1, 2
	
if Market:UserOwnsGamePassAsync(Player.UserId, Gamepass) then
	player.leaderstats.Pops.Value = player.leaderstats.Pops.Value + vip_Pops
else
	player.leaderstats.Pops.Value = player.leaderstats.Pops.Value + Pops
end
1 Like

The first one worked, thank you so much!

1 Like

Hello, I would like to reopen this solution because when a player without the gamepass touches a block, it gives them one pop which is what I want it to do, but now it is affected the gamepass players, it gives them also one pop.

And you are making sure to change the amount of pops for if they have a gamepass or not? Make sure your not using the same number of pops for each player.

I think I see an error here. See, you have a leaderstats item called Pops and that might be interfering with the variable pops. I see that you have pops which is a variable and a leaderboard item. Try changing the name to another thing such as default_Pops. It’s usually a good idea to name something similar. I tend to find it bad practice to call variables with simple names such as pops.