#Touched event problems

8 messages · Page 1 of 1 (latest)

true sable
#

Hi, I recently started learning Luau and decided to write a simple code for sliding doors. When the player stands on the button, the door opens, when the player gets off the button, the door closes.
I ran into the problem that the events are called too many times, which makes the door not work correctly.
I tried different ways to add "Debounce", it was certainly better, but still didn't work as I would like (For example, the door could just get stuck in the open position if the player managed to jump off the button within the DEBOUNCE_TIME. Or the door would start closing only after the DEBOUNCE_TIME period, not as soon as the player jumps off the button).

I would be very grateful if someone could tell me how to fix this.

#
-- Constants for clarity and maintainability
local DOOR_OPEN_TIME = 2
local DOOR_EASING_STYLE = Enum.EasingStyle.Quart
local DEBOUNCE_TIME = 0.25

-- References to objects
local leftDoor = script.Parent.LeftDoor
local rightDoor = script.Parent.RightDoor

local leftOpenGoalCFrame = script.Parent.LeftGoal.CFrame
local rightOpenGoalCFrame = script.Parent.RightGoal.CFrame

local leftCloseGoalCFrame = leftDoor.CFrame
local rightCloseGoalCFrame = rightDoor.CFrame

local trigger = script.Parent.Trigger

-- Services
local tweenService = game:GetService("TweenService")

-- State variables
local isOpen = false
local debounce = false

-- Function to open the doors
local function openDoors(hit)
    if not isOpen and not debounce then
        print("Door opening...")
        -- debounce = true
        isOpen = true

        -- Create tweens for smooth opening
        local leftDoorTween = tweenService:Create(leftDoor, TweenInfo.new(DOOR_OPEN_TIME, DOOR_EASING_STYLE), {CFrame = leftOpenGoalCFrame})
        local rightDoorTween = tweenService:Create(rightDoor, TweenInfo.new(DOOR_OPEN_TIME, DOOR_EASING_STYLE), {CFrame = rightOpenGoalCFrame})

        leftDoorTween:Play()
        rightDoorTween:Play()
        print("Door opened!")
    end
end

-- Function to close the doors
local function closeDoors(hit)
    if isOpen and not debounce then
        print("Door closing...")
        -- debounce = true
        isOpen = false

        -- Create tweens for smooth closing
        local leftDoorTween = tweenService:Create(leftDoor, TweenInfo.new(DOOR_OPEN_TIME, DOOR_EASING_STYLE), {CFrame = leftCloseGoalCFrame})  -- Use current CFrame for closing
        local rightDoorTween = tweenService:Create(rightDoor, TweenInfo.new(DOOR_OPEN_TIME, DOOR_EASING_STYLE), {CFrame = rightCloseGoalCFrame})

        leftDoorTween:Play()
        rightDoorTween:Play()
        print("Door closed!")
    end
end



trigger.Touched:Connect(openDoors)
trigger.TouchEnded:Connect(closeDoors)
#
-- References to objects and services
local partToAnimate = script.Parent
local trigger = script.Parent.Parent.Trigger
local clickSound = script.Parent.Parent.ClickSound
local tweenService = game:GetService("TweenService")

-- Constants for animation and debounce
local ANIMATION_TIME = 0.5
local EASING_STYLE = Enum.EasingStyle.Circular
local DEBOUNCE_TIME = 0.25

-- Initial position
local initialPosition = partToAnimate.Position

-- Tween information
local tweenInfo = TweenInfo.new(ANIMATION_TIME, EASING_STYLE)

-- Tweens for moving down and back up
local tweenDown = tweenService:Create(partToAnimate, tweenInfo, {Position = initialPosition + Vector3.new(0, -0.2, 0)})
local tweenUp = tweenService:Create(partToAnimate, tweenInfo, {Position = initialPosition})

-- State variable for debounce
local isDebounceActive = false

-- Function to handle part movement and sound on touch
local function animatePartOnTouch()
    if not isDebounceActive then
        isDebounceActive = true
        tweenDown:Play()
        clickSound:Play()

        task.delay(DEBOUNCE_TIME, function()
            isDebounceActive = false
        end)
    end
end

-- Function to handle part movement and sound on touch end
local function animatePartOnTouchEnd()
    if not isDebounceActive then
        isDebounceActive = true
        tweenUp:Play()
        clickSound:Play()

        task.delay(DEBOUNCE_TIME, function()
            isDebounceActive = false
        end)
    end
end

-- Connect events to functions
trigger.Touched:Connect(animatePartOnTouch)
trigger.TouchEnded:Connect(animatePartOnTouchEnd)

brisk pebble
#

hell no

#

is this a 200 line script for an opening door

true sable
limpid mistBOT
#

studio** You are now Level 1! **studio