Issue with multiple functions executing

local function handlePurchaseFinished(playerID, productId, wasPurchased)
	print("Purchase finished event fired")
	if wasPurchased then
				start.Touched:Connect(function(hit)
					teleportPlayer()
					print("Product purchased, so teleported")
				end)
				destination.Touched:Connect(function(hit)
					teleportPlayer2()
					print("Product purchased, so teleported")
				end)
			else
				print("Product not purchased, did not teleport player")
			end
		end

What do you want to achieve? I want a touch function to execute based on the part that is being touched. I want it to execute only once.

What is the issue? Assume that the start and destination constants are parts and that the teleportPlayer() function inside of start’s touch function leads the player to the destination part. As the player is then touching the destination part, destinations touch function will then execute.

I want to prevent this, I don’t want destination’s touch function executing, I want only a maximum of one touch function executing. The order/location (line) of the touch function does not matter.

If part A is touched, execute its respected touch function.

If part C is touched, execute its respected touch function.

No other touch function should fire. Like, what I mean is: End the script when a single touch function is executed - that’s what I want to achieve.

What solutions have you tried so far? Nothing, I am relying on AI to code. Research hasn’t helped.

2 Likes

Hello, I didn’t really understand the problem. The issue is that the function executes more than once at a time?

I couldn’t find a suitable name for this topic by the way but,

Let me make it more simpler:

Imagine you have 3 parts:

  • Part A
  • Part B
  • Part C.

When you touch Part A, you are teleported to Part B (how the teleportation happens is irrelevant, don’t worry, you don’t need that information). If Part B is touched, you teleport to Part C.

I don’t want Part B to teleport me to Part C if I was teleported to Part B by Part A.

Meaning, I don’t want my final stop/position to be Part C. I want it to be Part B, but I don’t know how I can achieve that.

However, also, if I never touched part A, and just touched Part B, then I want my final destination to be Part C (I want Part B’s touch function to work in that case).

So, what I’m trying to say is:

I don’t want A to B to C, I want A to B.

And I also accept/want B to C.

That’s what I’m trying to achieve. It’s hard to explain, I’m sorry if I’m still confusing you.

I think I understood the problem. So, you can make a variable that will become true if the player got teleported to part B from part A. If it’s true and the player wants to teleport to part C, he can’t and if it’s false, he can.

1 Like

Thanks, I’ll get to work on it, if it works out, I’ll mark your response a solution.

No problem, I hope you will solve this problem.

1 Like

I suppose your solution is technically correct, but I’m kind of having trouble applying your idea.

local um = false
local function handlePurchaseFinished(playerID, productId, wasPurchased)
	print("Purchase finished event fired")
	if wasPurchased then
		if um == false then 
				start.Touched:Connect(function(hit)
					teleportPlayer()
				print("Product purchased, so teleported")
				um = true
			end)
			else
				destination.Touched:Connect(function(hit)
					teleportPlayer2()
					print("Product purchased, so teleported")
			end)
			end
			else
				print("Product not purchased, did not teleport player")
			end
		end

video snipped after solution for privacy

Observe this demonstration. Imagine I am implementing a skipping system for a guess the logo game.

The full code is basically for a text button that teleports you to a part if you purchase a dev product. My goal is for the destination of the teleportation to vary based on what the player is touching.

Part A is in front of logo 1. Part B is in front of logo 2 and Part C is in front of logo 3.

If the player goes from A to B, they don’t end up in C (0:00 to 0:13). Okay, that’s what I wanted. And they can go from B to C if they want to after being teleported from A to B (0:14-0:20), that’s fine with me as well.

But I’m really not able to just directly go from B to C (rest of the vid)

Also, I plan to extend the number of parts in my problem (from A to Z for instance instead of A to C), with, that many parts, I don’t think your solutions the best choice for that, the A to C example was just for simplicity, I apologize for not providing enough details. I should’ve said more.

Look, I think, you can make a table “doors” with all doors named from “1” to “10” (I don’t know how much doors you have) and a variable “current door”, if your gamepass is purchased, then you go to the current door + 1 + center position of the room and your current door variable becomes 2.

From what i understood, you only have to keep a track of your previous teleports points so you can check if the player is able to teleport at the C point or not.

local PreviousTeleportPoints = {}

--Teleport A to B
PreviousTeleportPoints = {"A", "B")

--Can Teleport from B to C?
if not (PreviousTeleportPoints[1] == "A") and not (PreviousTeleportPoints[2] == "B") then
    --Teleport B to C // only if the previous teleport wasn't A to B
    PreviousTeleportPoints = {"B", "C")
end
local doors = {game.workspace.1, game.workspace.2, game.workspace.3} -- A table where are all of your doors in a quiz.
local humroot = Character.HumanoidRootPart -- HumanoidRootPart
local currentdoor = 1 -- The door you are on

if purchased then -- If you bought the gamepass
Character.humroot.Position = table.find[currentdoor+1].Position + Vector3.new(-50, 0, 0) -- then you teleport to the next door that you didn't go through yet
currentdoor = currentdoor + 1 -- makes that your current room is now the door you just teleported, so the next door will be third if you teleported to second, etc.
1 Like

I would like to sincerely thank you for your time and assistance. Using what you provided, I coded:

local doors = {game.Workspace:WaitForChild("first"), game.Workspace:WaitForChild("seconds"), game.Workspace:WaitForChild("third")} -- A table where are all of your doors in a quiz.
local currentdoor = 1 -- The door you are on

local function handlePurchaseFinished(playerID, productId, wasPurchased)
	print("Purchase finished event fired")
	if wasPurchased  then
		character:PivotTo(CFrame.new(doors[currentdoor+1].Position))-- then you teleport to the next door that you didn't go through yet
		currentdoor = currentdoor + 1 -- makes that your current room is now the door you just teleported, so the next door will be third if you teleported to second, etc.
print("Product purchased, teleported player")
	else
		print("Product not purchased, did not teleport player")
	end
end

It’ll do, it works perfectly, thank you.

I’ll just use a different developer product and textbutton for each logo category and put all their respected door names in the table. Once again, thank you.

1 Like