Teleporter doesn't send player to correct part


*In the video above, I press yes on the confirm prompt and it teleports me to my destination. But when I try it with the other part to return back, it teleports me to the first destination.

The code I have is a module script that is accessed via another module script to do actions because of my game’s framework → local script with a remote event firing.*


Here is the Module Script code(snippet)

local RES = game:GetService('ReplicatedStorage')
local remotes = RES.Remotes

local interactableTypes = {
        -- don't worry about Test & Gamepass. Focus on `Prompt`
	Test = function(plr, obj)
		print(`{plr.Name} has tested for the object: {obj.Name} | {plr.Name} && {obj.Name}`)
	end,
	
	Gamepass = function(plr, obj)
		if obj.Name == 'Gamepass' then
			local gamepassId = obj:FindFirstChild('GamepassID') :: StringValue
			print(`{plr.Name} | {gamepassId.Value}`)
			
			MPS:PromptGamePassPurchase(plr, tonumber(gamepassId.Value))
		end
	end,
	
	Prompt = function(plr, obj)
		remotes.SC_PromptPopupTP:FireClient(plr, obj)
	end,
}

--[=[
_G is not used for Lib folder.
To require modules in Lib folder, use require() function
]=]--

local Lib__Interactable = {}
Lib__Interactable.__index = Lib__Interactable

function Lib__Interactable.New(typeName: string, object: Instance, activationDistance: number, actionText: string)
	local self = setmetatable({
		
	}, Lib__Interactable)
	
	self._object = object
	
	------------
	
	self._proximityPrompt = Instance.new('ProximityPrompt')          :: ProximityPrompt
	self._proximityPrompt.Parent = self._object                      :: any
	self._proximityPrompt.MaxActivationDistance = activationDistance :: number
	self._proximityPrompt.ActionText = actionText                    :: string
	
	self._proximityPrompt.Triggered:Connect(function(plr)
		interactableTypes[typeName::string](plr, self._object)
	end)
	
	return self
end

return Lib__Interactable

Here is the Module Script code to access the above module(snippet)

Interactable.New('Prompt', workspace.Interactables.Shop, 10, `bob's shop is cool`)
Interactable.New('Prompt', workspace.Interactables.ShopReturn, 10, `go back to wolrd`)

And finally here is the Local Script code(snippet ofc)

remotes.SC_PromptPopupTP.OnClientEvent:Connect(function(obj)
	prompt.Visible = true
	promptText.Text = obj:WaitForChild('PromptText').Value :: StringValue
	
	yesButton.MouseButton1Click:Connect(function()
		prompt.Visible = false
		
		local dest = obj:FindFirstChild('Destination').Value
		
		print(dest)
		
		hrp.CFrame = CFrame.new(dest.Position :: ObjectValue)
	end)
    -- code used when u press the no button
end)

Explorer for any needed help

image


The ObjectValue Destination is the object that I want the player to go to.


For ShopReturn(the teleporter that was bugged) has it’s Destination ObjectValue set to Dest
image


And these are my RemoteEvents stored in ReplicatedStorage
image


It would be very much appreciated if anybody could help me with this issue as teleporting is a big factor in the game I’m developing. :slight_smile:

1 Like

It looks like the player is getting teleported twice at 9 seconds in the video:

image
The console is printing both Dest and TPDestination. To make it more obvious, you can try printing the .Position.


That then makes me assume that the following is fired twice:

remotes.SC_PromptPopupTP.OnClientEvent:Connect(function(obj)

Could you add a print statement below that and see if it fires twice?


And, for that to be firing twice, it looks like the proximity prompt needs to be ‘fired’/interacted with twice as each time there is an interaction that function is fired. So, maybe the same proximity prompt has multiple objects, but that doesn’t seem to be the case based on the code I’m reading here:

function Lib__Interactable.New(typeName: string, object: Instance, activationDistance: number, actionText: string)
	local self = setmetatable({
		
	}, Lib__Interactable)
	
	self._object = object
	
	------------
	
	self._proximityPrompt = Instance.new('ProximityPrompt')          :: ProximityPrompt
	self._proximityPrompt.Parent = self._object                      :: any
	self._proximityPrompt.MaxActivationDistance = activationDistance :: number
	self._proximityPrompt.ActionText = actionText                    :: string
	
	self._proximityPrompt.Triggered:Connect(function(plr)
		interactableTypes[typeName::string](plr, self._object)
	end)
	
	return self
end

I hope that helps, my best advice is to add print statements throughout the process the code is taking. See where it stops printing two value, and why it printing two values at the end.

I added print statements for when the RemoteEvent fires and for the position of the Destination.


I honestly have no idea what’s happening because I only fire the RemoteEvent SC_PromptPopupTP once.


image

This code fires the RemoteEvent.


image

Then this snippet sees if the player pressed the Yes button on the prompt and prints “fired” but it only prints once because it’s only fired once and not twice. I don’t think it’s something to do with my ObjectValue but I digress.

1 Like

Could you add a line under where the player gets teleported to disconnect the event?

yesButton.MouseButton1Click:Connect(function() is firing twice as it looks like the old firing of the first teleport is still connected to it, meaning that the yesButton function would be firing twice.

I assume the same thing would be happening for the noButton function.

how would I disconnect the event? Would I create a new variable and then :Disconnect() it?


So I tried to make a connection thread w/ the documentation you sent in the hyperlink:

But after clicking the No button, the yes button completely stops working.


local connection

remotes.SC_PromptPopupTP.OnClientEvent:Connect(function(obj)
	print('fired')
	prompt.Visible = true
	promptText.Text = obj:WaitForChild('PromptText').Value :: StringValue
	
	local function click()
		prompt.Visible = false

		local dest = obj:FindFirstChild('Destination').Value
		print(dest.Position)

		hrp.CFrame = CFrame.new(dest.Position :: ObjectValue)
		connection:Disconnect()
	end
	
	connection = yesButton.MouseButton1Click:Connect(click)
    -- other code
end)

1 Like

I think it would be like the following:

local connection

local function nameHere()
	prompt.Visible = false
	local dest = obj:FindFirstChild('Destination').Value
	print(dest)
    hrp.CFrame = CFrame.new(dest.Position :: ObjectValue)
    connection :Disconnect()
end)

connection = yesButton.MouseButton1Click:Connect(nameHere)

I tried that and it doesn’t work as shown in the video above. :frowning:

1 Like

At least the yesButton stuff is working now. :sweat_smile:

If the noButton stuff is not working, then I think the noButton function also needs to have the same disconnection stuff to happen:

local connectionYes
local connectionNo

remotes.SC_PromptPopupTP.OnClientEvent:Connect(function(obj)
	print('fired')
	prompt.Visible = true
	promptText.Text = obj:WaitForChild('PromptText').Value :: StringValue
	
	local function clickYes()
		prompt.Visible = false

		local dest = obj:FindFirstChild('Destination').Value
		print(dest.Position)

		hrp.CFrame = CFrame.new(dest.Position :: ObjectValue)
		connection:Disconnect()
	end

    local function clickNo()
        -- Code for the no button goes here
    end
	
	connectionYes = yesButton.MouseButton1Click:Connect(clickYes)
    connectionNo = noButton.MouseButton1Click:Connect(clickNo)

end)

The code isn’t break-ing out of the function above it I don’t think, so I’m not sure why the no button would stop working. I’d add a print statement to see if the code runs after the disconnections. They should, but yeah.

it still acts the same :confused:


this is the noButton code:

noButton.MouseButton1Click:Connect(function()
	prompt.Visible = false
	if obj:FindFirstChild('NPC') then
		local npc = obj:FindFirstChild('NPC').NPC :: Model

		local msgs = {
			`You scammer!`,
			`You'll pay for this!`,
			`I know who u are {plr.Name}`,
			`Why u do this to me :(`,
			`You noob!`
		}

		interactable:ChatNPC(npc:FindFirstChild('Head'), msgs[math.random(1, #msgs)], 'Red')

		local face: Decal = npc:FindFirstChild('Head'):FindFirstChild('face')

		face.Texture = 'rbxassetid://910076130'
		RES.Sounds.Boom:Play()

		task.wait(2)

		face.Texture = 'rbxasset://textures/face.png'
	end
end)
1 Like

Could you send a .rbxl file with just the relevant code and assets? I could try to reproduce the set up manually, but that would take quite a while.

Sure!


CoolProject.rbxl (159.5 KB)

The Interactable script is in ReplicatedStorage → Lib → Interactable
The ClientHandler script is in StarterGui → Client → ClientHandler

1 Like

I seem to have fixed the issue by disconnecting both events right after either of them fire. If both are not deleted when one is pressed, they accumulate over time, causing issues.

CoolProject v2.rbxl (159.5 KB)

Had to make it a YouTube video bc forum has 10mb limit.

Thank you so much for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.