Hello, just like the title says, I’ve been working on a code that would open the closest door to the player if he presses E (First Enabling a GUI that says “Press E to interact”, then waiting for user input E key to fire a remote event, check the magnitude again and fire back to all clients to tween the door).
My issue as of now is that it semi-breaks when there is more than 1 door, the GUI being set from false to true, same with the function calling. I will attach the code below:
Local Script as child of a GUI in StarterGui:
local UIS = game:GetService("UserInputService")
local Doors = game:GetService("Workspace"):FindFirstChild("Doors")
local Interaction_Gui = script.Parent
local DoorEvent = game.ReplicatedStorage.RemoteEvents.DoorEvent
local TS = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(1.3, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, true)
local tweeninfo2 = TweenInfo.new(2.3, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, true)
local function Interaction(door)
local connection = nil
connection = UIS.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E and not gameProcessed then
local Open_bool = door.Open.Value
if not Open_bool then
DoorEvent:FireServer(door)
Interaction_Gui.Enabled = false
print(door.Name)
connection:Disconnect()
end
end
end
end)
end
game:GetService("RunService").Heartbeat:Connect(function()
for _,Interactable in pairs(Doors:GetChildren()) do
local Magnitude = (Interactable.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
if Magnitude <= 10 and Interaction_Gui.Enabled == false then
Interaction_Gui.Enabled = true
Interaction(Interactable)
print("called interaction function")
elseif Magnitude > 10 and Interaction_Gui.Enabled == true then
Interaction_Gui.Enabled = false
print("False")
end
end
end)
DoorEvent.OnClientEvent:Connect(function(Door)
if Door.Name == "Door" then
local goal = {CFrame = (Door.CFrame * CFrame.new(-6.3,0,0))}
local TweenOpen = TS:Create(Door, tweeninfo, goal)
TweenOpen:Play()
print("opening door")
elseif Door.Name == "Big Door" then
local goal = {CFrame = (Door.CFrame * CFrame.new(-8.3,0,0))}
local TweenOpen = TS:Create(Door, tweeninfo2, goal)
TweenOpen:Play()
end
end)