I am trying to make it so whenever I’m in proximity of a door, of x studs, for example, and I hover my mouse over the door, it turns to another mouse icon - I have that part down. The next part is, while still in proximity, and while still having my mouse over it, I want to press a keybind to handle the opening of the door and playing of a sound, etc. How would I go about doing this? Here is my current code.
local doorPart = workspaceService:FindFirstChild('door1')
local mouseIcon = 'rbxassetid://2383357875'
local whitelistedDoors = {
[workspaceService:WaitForChild("door1")] = true
}
local localPlayer = playersService.LocalPlayer
local localCharacter = localPlayer.Character
local localHumanoid = localCharacter:WaitForChild('Humanoid')
local playerMouse = localPlayer:GetMouse()
runService.RenderStepped:connect(function()
if localPlayer:DistanceFromCharacter(doorPart.Position) <= 15 then
local Target = playerMouse.Target
if Target and whitelistedDoors[Target] then
playerMouse.Icon = mouseIcon
else
playerMouse.Icon = ''
end
end
end)
while wait(.1) do
if localPlayer:DistanceFromCharacter(doorPart.Position) <= 15 and (Target and whitelistedDoors[Target]) then
userInputService.InputBegan:connect(function(input, gameProcessed)
if not gameProcessed then
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
-- Handle opening of the door here
end
end
end
end)
end
end
EDIT: Right now the observed behavior is that if I add a print inside the keybind check, it prints something multiple times. I want it to only execute once so that if I open the door, the door function isn’t executed multiple times, just once for opening/closing.