Button Combination system

How would i go about making a button combination that makes you teleport when a combination is made. But when the combination is wrong for my 3 buttons it does not work. And resets it, so the combination can be retried

My code did not work at all, looking to completely scrap this. And it uses a proximity prompt

-- Configuration
local buttonNames = {"Button1", "Button2", "Button3"}
local correctCombination = {1, 3, 2} 
local teleportDestination = game.Workspace.TeleportDestination 

-- Variables
local currentCombination = {}
local resetDelay = 5

local function isCorrectCombination()
    if #currentCombination ~= #correctCombination then
        return false
    end

    for i = 1, #currentCombination do
        if currentCombination[i] ~= correctCombination[i] then
            return false
        end
    end

    return true
end


local function resetCombination()
    currentCombination = {}
end

local function teleportPlayer()
    game.Players.LocalPlayer.Character:SetPrimaryPartCFrame(teleportDestination.CFrame)
    resetCombination()
end

local function onButtonPress(buttonName)
    table.insert(currentCombination, tonumber(string.sub(buttonName, -1)))  

    if isCorrectCombination() then
        teleportPlayer()
    else
        wait(resetDelay)
        resetCombination()
    end
end


for _, buttonName in ipairs(buttonNames) do
    local button = game.Workspace:WaitForChild(buttonName)
    button.Touched:Connect(function(hit)
        local character = hit.Parent
        if character and character:IsA("Model") and character:FindFirstChild("Humanoid") then
            onButtonPress(buttonName)
        end
    end)
end
1 Like

I am writing a new script so you can use it, but just one thing. Do you want the code to be secure? Because then you would have to make a remote function that sends to server to check if the code is right. If you don’t do this, then exploiters can easily steal the code. But anyways, at that point, they can just manipulate the code so they can just teleport past. So do you care about having a remote event for verification? Also, can you tell me if you want the whole server teleported, or only the player that did it?

1 Like

I think this should be about it for a local script : )

local buttonNames = {"Button1", "Button2", "Button3"}
local buttonNamesLocation = workspace

local correctCombination = "123"

local teleportDestination = game.Workspace.TeleportDestination 
local resetDelay = 5

--------------------------------------------------------------------------------

local debounce = false

local correctCombinationLength = string.len(correctCombination)
local currentCombination = ""

function gotCorrectCombination()
	
	game.Players.LocalPlayer.Character:SetPrimaryPartCFrame(teleportDestination.CFrame)
	-- put other code to execute if the player gets the combination right here
	-- since this script probably won't be reused again, I don't know the context for it but I assume so, you can just run script:Destroy() after or right before this comment to save memory and resources.
end

function isCombinationCorrect()
	if currentCombination == correctCombination then
		return true
	else
		return false
	end
end

function resetCombination()
	currentCombination = ""
	debounce = true
	
	-- Run other reset combination code here
	
	task.wait(resetDelay)
	debounce = false
end

function pressedButton(buttonName)
	if debounce then return end
	
	currentCombination = currentCombination .. string.sub(buttonName, -1)
	if string.len(currentCombination) >= correctCombinationLength then
		if isCombinationCorrect() then
			gotCorrectCombination()
		else
			resetCombination()
		end
	end
end

for _,v in ipairs(buttonNames) do
	local button = buttonNamesLocation:WaitForChild(v.Name)
	
	button.Touched:Connect(function(hit)
		local character = hit.Parent
		if game.Players.LocalPlayer.Character == character and character:FindFirstChild("Humanoid") then
			pressedButton(v.Name)
		end
	end)
end

This code above SHOULD work, if there is a bug or it isn’t working, just reply and tell me what the error is and I will fix it in a little.

2 Likes

sorry for the late response, its just for the person who completed it, ill check out your code. Thanks so much!

1 Like

I tried this script in workspace. both starterplayers too. None if it worked, i thought it would work with proximity prompts, but what am I to say. I am not getting errors either…

If you are using proximity prompts, replace

button.Touched:Connect(function(hit)

with

button.PromixityPrompt.Triggered:Connect(function(hit)

make sure that the ProximityPrompts is being referenced in this line. Sorry, I was basing it off the original script and it said .Touched so I just went with it. Make sure that the proximity prompt is inside of the buttons.

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