:Disconnect() doesn't work to disable function

So I have a gamepass and want it to give player 10 more coins then they usually get per win they get so (20 coins). But instead it doesn’t work and it doesn’t put any errors in the output. Here is the script:

local function givewin(player)
	player.leaderstats.Wins.Value += 1
	player.leaderstats.Coins.Value += 10
end

local function TWOX(player)
	player.leaderstats.Coins += 20
	player.leaderstats.Wins += 1
end

local function givewinfunction()
	local twoX = 25789902
	wait(1)
	game.Players.PlayerAdded:Connect(function(player)
		if game:GetService("MarketplaceService"):UserOwnsGamepassAsync(player.UserId,twoX) then
			givewin:Disconnect()
			wait()
			TWOX()
		else
			TWOX():Disconnect()
			wait()
			givewin()
		end
	end)
end

function gMgr:declareWinner(player)
	givewinfunction()
	wait()
	winnerRe:FireAllClients(player, declareWinnerTime)
	wait(declareWinnerTime)
	sessionData[player]["isWinner"] = true
	sessionData[player]["win"] = sessionData[player]["wins"] + 1
	for ply, data in pairs(sessionData) do
		local spawnPnt = lSpawns[math.random(1, #lSpawns)]
		ply.Character.HumanoidRootPart.CFrame = spawnPnt.CFrame + Vector3.new(0, 3, 0)
		data["isPlaying"] = false
	end
end

Functions can’t be disconnected. Only roblox signals can.

2 Likes

Then how am I suppose to stop the function from running then?

Why would you need to? You’re only adding values to a player (which happens more or less instantly anyway)

I said that if a player owns a gamepass they should get double the coins then regular so they will get 20 coins and this is the way I am trying to figure it out.

It’s not looping and only happens once. Your script functions fine without needing to stop the function.

But it doesn’t work… what should I do…

Remove the lines with :Disconnect()

Ok. But it still doesn’t work I just don’t know why. Output still says nothing.

Likely because there are no print statements…

It’s probably because that code will execute for only new players that join the game, excluding any players that are already in the game. (I’m assuming you don’t want that?)

So instead, grab all the players in the game instead of waiting for new players to join:

local function givewinfunction()
	local twoX = 25789902
	wait(1)
	for _, player in ipairs(game.Players:GetPlayers()) do
		if game:GetService("MarketplaceService"):UserOwnsGamepassAsync(player.UserId,twoX) then
			givewin:Disconnect()
			wait()
			TWOX()
		else
			TWOX():Disconnect()
			wait()
			givewin()
		end
	end
end

Your script doesn’t work sad : (

To disconnect a function you need to set them as a variable: Handling Events go to “Disconnecting a Function”

I think you forgot to use .Value on lines 7 and 8.

local function TWOX(player)
	player.leaderstats.Coins.Value += 20 <<<
	player.leaderstats.Wins.Value += 1 <<<
end

To begin with, you need to comprehend the topic thoroughly. There might be more than one solution so let me give some examples of how you may achieve it:

Inside the Player
  • Every time a player joins the server, just create a value that you can store an identification, which could either be a number or a string or a boolean, preferable.
game:GetService("Players").PlayerAdded:Connect(function(player) -- The player
    local value = Instance.new("BoolValue") -- BoolValue recommended

    local success, returned = pcall(function()
        return game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, gamepassId)
    end)

    if success and returned then
        value.Value = returned
    else
        value.Value = false
    end

    value.Parent = player
end)
Inside a Table (Recommended)

When the server starts off running, create a table where you can store an identification for each player in the server, which could be either a number or a string or a boolean, preferable.

local list = {}

game:GetService("Players").PlayerAdded:Connect(function(player) -- The player
    local success, returned = pcall(function()
        return game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, gamepassId)
    end)

    if success and returned then
        list[player] = returned -- I wanted to store boolean as it makes it easier to read.
    else
        list[player] = false
    end
end)

And when you want a player to win and multiply the increase by two, acquire the table to continue with it.

local function awardPlayer(player)
    if list[player] then
	    player.leaderstats.Wins.Value += 1
	    player.leaderstats.Coins.Value += 20
    else
        player.leaderstats.Wins.Value += 1
	    player.leaderstats.Coins.Value += 10
    end
end

The rest of the script goes here.

function gMgr:declareWinner(player)
    awardPlayer(player)

   	winnerRe:FireAllClients(player, declareWinnerTime)
	wait(declareWinnerTime)
	sessionData[player]["isWinner"] = true
	sessionData[player]["win"] = sessionData[player]["wins"] + 1
	for ply, data in pairs(sessionData) do
		local spawnPnt = lSpawns[math.random(1, #lSpawns)]
		ply.Character.HumanoidRootPart.CFrame = spawnPnt.CFrame + Vector3.new(0, 3, 0)
		data["isPlaying"] = false
	end
end

There is one more thing. Unregister players from the table if they quit the game.

game:GetService("Players").PlayerRemoving:Connect(function(player)
    table.remove(list, player)
end)
  • Note: Implement the awardBadge function accordingly in the first example by simply replacing the list with values that are children of the player.

As I said, those are some examples but there may be thousands of it. It depends on what you want to do.

I will end this up by summarizing it; Disconnect() seems redundant here.

1 Like

I don’t understand list that is in your script. My script doesn’t know what the list is and giving an error out about list.

I’m really sorry; I noticed that I mistyped the list variable. Fixed it. I assume it will work properly.

It doesn’t work :frowning: maybe its studio that’s not working?

You need to disconnect the function, then make it nil. (Unless it is a round-based system.)

Try this?

local function givewin(player)
    -- don't use +=, not valid Lua syntax, instead reference the old value again
	player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10
end

local function TWOX(player)
    -- don't use +=, not valid Lua syntax, instead reference the old value again
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 20
	player.leaderstats.Wins = player.leaderstats.Wins.Value + 1
end

local function givewinfunction()
	local twoX = 25789902
	wait(1)
    -- instead of waiting for new players, iterate over current players, like what sharpie said
    for k,v in pairs(game.Players:GetPlayers()) do
         if game:GetService("MarketplaceService"):UserOwnsGamepassAsync(v.UserId,twoX) then
               TWOX(v)
         else
               givewin(v)
         end
    end
end

You don’t need to disconnect the function. Note that functions cannot be disconnected, only signals can. I’m assuming, if I’m understanding your situation correctly, that you just don’t want to call the function for that particular player … in that case, just do nothing like what I did above.