Search code examples
luaroblox

Change from click detect to proximity prompt


I tried a variety of things to try and swap this from click to proximity. Not too sure. I figured since it's proximity, you'd make a separate script, but even doing that, was still giving me troubles. Instead of clicking the button (it's a FPS game), I want it to do the same exact thing, but using proximity prompt so players can "click" to button by hitting the "e" key. Want to learn! Thanks in advance.

local tweenService = game:GetService("TweenService")
local event = Instance.new("BindableEvent")

local Door = script.Parent
local DoorMain = script.Parent.Parent.Frame
local TweenedCompleted = "N/A"
local DoorStatus = "Closed"
local Debounce = false

local function tweenModel(model, cframe, time)
    local cframeValue = Instance.new("CFrameValue")
    cframeValue.Value = model:GetPrimaryPartCFrame()

    cframeValue:GetPropertyChangedSignal("Value"):connect(function()
        model:SetPrimaryPartCFrame(cframeValue.Value)
    end)

    local info = TweenInfo.new(time, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
    local tween = tweenService:Create(cframeValue, info, {Value = cframe})
    tween:Play()

    tween.Completed:connect(function()
        cframeValue:Destroy()
    end)
end

local cframes = {
    door1orig = Door.Door1.Center.CFrame,
    door1TGP = Door.Door1.ToGoPos.CFrame,
    door2orig = Door.Door2.Center.CFrame,
    door2TGP = Door.Door2.ToGoPos.CFrame
}

local function Open()
    tweenModel(script.Parent.Door1, cframes.door1TGP, 5)
    wait(2.35)
    tweenModel(script.Parent.Door2, cframes.door2TGP, 2.65)
    wait(3)
end

local function Close()
    tweenModel(script.Parent.Door1, cframes.door1orig, 5)
    tweenModel(script.Parent.Door2, cframes.door2orig, 3)
    wait(5)
end
--//Toggle event//--
script.Parent.Button1.ClickDetector.MouseClick:Connect(function()
    if DoorStatus == "Closed" and Debounce == false then
        Debounce = true
        Door.Button1.Click:Play()
        DoorMain.Sound:Play()
        Open()
        wait(1)
        DoorStatus = "Opened"
        Debounce = false
    elseif DoorStatus == "Opened" and Debounce == false then
        Debounce = true
        Door.Button1.Click:Play()
        DoorMain.Sound:Play()
        Close()
        wait(1)
        DoorStatus = "Closed"
        Debounce = false
    end
end)

script.Parent.Button2.ClickDetector.MouseClick:Connect(function()
    if DoorStatus == "Closed" and Debounce == false then
        Debounce = true
        Door.Button2.Click:Play()
        DoorMain.Sound:Play()
        Open()
        wait(1)
        DoorStatus = "Opened"
        Debounce = false
    elseif DoorStatus == "Opened" and Debounce == false then
        Debounce = true
        Door.Button2.Click:Play()
        DoorMain.Sound:Play()
        Close()
        wait(1)
        DoorStatus = "Closed"
        Debounce = false
    end
end)

Solution

  • Place a proximity prompt in the buttons. Connect the .Triggered event of the proximity prompt to a function. It should look something like this

    script.Parent.Button2.ProximityPrompt.Triggered:Connect(function()
    
    end)
    

    After you are done changing the events you can customize the proximity prompt.