Swap players locations using proximity prompt

I want to have 2 prompts and when 2 diffrent players activate it it will wait 3 seconds and the swap their positions/locations it doesn’t work here is my current code:

local TpButtons = script.Parent
local bluePressed = TpButtons:FindFirstChild("BluePressed")
local orangePressed = TpButtons:FindFirstChild("OrangePressed")
local blueTp = TpButtons:FindFirstChild("BlueTp")
local orangeTp = TpButtons:FindFirstChild("OrangeTp")

local function swapPlayers()
    if bluePressed.Value and orangePressed.Value then
        local bluePlayer = blueTp.TpButton:FindFirstChildOfClass("ProximityPrompt").LastTriggered
        local orangePlayer = orangeTp.TpButton:FindFirstChildOfClass("ProximityPrompt").LastTriggered
        if bluePlayer and orangePlayer then
            local blueRoot = bluePlayer.Character and bluePlayer.Character:FindFirstChild("HumanoidRootPart")
            local orangeRoot = orangePlayer.Character and orangePlayer.Character:FindFirstChild("HumanoidRootPart")
            if blueRoot and orangeRoot then
                local bluePosition = blueRoot.Position
                local orangePosition = orangeRoot.Position
                blueRoot.CFrame = CFrame.new(orangePosition)
                orangeRoot.CFrame = CFrame.new(bluePosition)
                print("Swapped positions of", bluePlayer.Name, "and", orangePlayer.Name)
            end
        end
    end
end

local function onProximityPromptTriggered(prompt, player)
    local character = player.Character
    local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
    if humanoidRootPart then
        if prompt.Parent == blueTp.TpButton then
            bluePressed.Value = true
            orangePressed.Value = false
        elseif prompt.Parent == orangeTp.TpButton then
            orangePressed.Value = true
            bluePressed.Value = false
        end
        swapPlayers()
    end
end

if blueTp and orangeTp then
    local bluePrompt = blueTp.TpButton:FindFirstChildOfClass("ProximityPrompt")
    local orangePrompt = orangeTp.TpButton:FindFirstChildOfClass("ProximityPrompt")
    
    if bluePrompt and orangePrompt then
        bluePrompt.Triggered:Connect(function(player) onProximityPromptTriggered(bluePrompt, player) end)
        orangePrompt.Triggered:Connect(function(player) onProximityPromptTriggered(orangePrompt, player) end)
    else
        warn("ProximityPrompt not found in one of the TpButtons")
    end
else
    warn("BlueTp or OrangeTp not found in TpButtons folder")
end
2 Likes

What is this? Is it an object value under the proximity prompt?

2 Likes

I don’t really know this isn’t the original code i asked chatgpt to make it work and it doesn’t work

ChatGPT is primarily language-based so doing logic-based stuff isn’t its strong suit. If you code use bing copilot it updates in real time and is somewhat more intelligent. Can you send the hierarchy of TpButtons? You can also send the TpButtons as a model and I’ll see what I can do.

1 Like

Try storing the players position in a table outside the functions

In addition to @sonic_848 ‘s point, this also makes it so that the two values will never be true at the same time. Definitely dont have chatgpt writing scripts for you.

1 Like

here you go:
tp buttons.rbxm (10.0 KB)

Well in this line here:

if bluePressed.Value and orangePressed.Value then

you aren’t actually checking anything. Here is what it should look like

if bluePressed.Value == true orangePressed.Value == true then

you can change that to fakse or true depending on what you need but I hope you understand that what I’m trying to say I’d when you say that if statement you are basically saying if blue pressed.value. But what about the value?

Hope this helps!

1 Like

i did it nothing has changed really lol

This same thing happens multiple times in the code and the teleport is written wrong.
There are heaps of things done wrong It’s not a good piece of code.

do you want me to try to re-write it for you?

yea it would be great i will be inactive in like 20 minutes tho

okay I’ll try my best in 20 mins

nvm im going inactive now you can rewrite anytime you want ill respond in like 8 hours :frowning: cuz of school

1 Like

Try this:

local TpButtons = script.Parent
local bluePressed = TpButtons:FindFirstChild("BluePressed")
local orangePressed = TpButtons:FindFirstChild("OrangePressed")
local blueTp = TpButtons:FindFirstChild("BlueTp")
local orangeTp = TpButtons:FindFirstChild("OrangeTp")
local bluePp = TpButtons:FindFirstChild("BlueProximityPrompt") -- change this to be the name of your blue proximity prompt
local OrangePp = TpButtons:FindFirstChild("OrangeProximityPrompt") -- change this to be the name of your orange proximity prompt

BluePp.Triggered:Connect(function(plr)
plr.Character:PivotTo(CFrane.new(blueTp.Position))
end
)
OrangePp.Triggered:Connect(function(plr)
plr.Character:PivotTo(CFrane.new(orangeTp.Position))
end
)

You totally understood it wrongly, there is 2 buttons when diffrent persons activate diffrent buttons that i talked about their locations/positions get swapped

Sooo, do both players need to press the buttons or just one?

both players need to press diffrent buttons to swap their location

Buttons? or proximity prompts?

that works as intended, there’s no difference between his line of code and your line of code except yours is longer

1 Like