The way I achieved a door opening on click was with Hinge Constraints.
If you set up the hinge constraints, then when you click, you just have to change the Orientation on the Y axis.
Here is the script I used:
local speed = 4
local increment = 0
local direction = false -- false = left, true = right
local debounce = false
local door = script.Parent
script.Parent.ClickDetector.MouseClick:Connect(function()
if debounce == false then
debounce = true
increment = direction and 2 or -2
for i = 1,10 do
wait()
door.Orientation = door.Orientation + Vector3.new(0,increment,0) * speed
end
direction = not direction
debounce = false
end
end)
Feel free to use my script and if you have questions let me know.
That looks very neat but I am trying to make it open on key. Like I hover my mouse over the door, a UI pops up with the key âFâ and then I press it and the door will open.
-- LocalScript in your UI
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenDoor = ReplicatedStorage:WaitForChild("OpenDoor") -- RemoteEvent
local DialogController = script.Parent.Parent.DialogController
local Choices = script.Parent.Parent.Choices
local DialogQuestion = script.Parent.Parent.DialogQuestion
UserInputService.InputBegan:Connect(function(InputObject, Processed)
if not Processed then
if InputObject.KeyCode == Enum.KeyCode.F then
OpenDoor:FireServer()
end
end
end)
--This would be inside your door, a ServerScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenDoor = ReplicatedStorage:WaitForChild("OpenDoor") -- RemoteEvent
local speed = 4
local increment = 0
local direction = false -- false = left, true = right
local debounce = false
local door = script.Parent
OpenDoor.OnServerEvent:Connect(function()
if debounce == false then
debounce = true
increment = direction and 2 or -2
for i = 1,10 do
wait()
door.Orientation = door.Orientation + Vector3.new(0,increment,0) * speed
end
direction = not direction
debounce = false
end
end)
Yes, you would need to create a RemoteEvent, I edited my reply to have both the Client Side Script and the Server Side Script.
I also went ahead and reformatted your SoundPlaying Script. (nothing wrong with how you had it, but this is a little more efficient.
local Sound = script.Parent.Sound
local Sound2 = script.Parent.Sound2
script.Parent.CLICKD1.MouseClick:Connect(function()
Sound:Play()
wait(2.5)
Sound2:Play()
end)
--ServerScript Place this in your door with the clickdetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")
script.Parent.ClickDetector.MouseHoverEnter:connect(function(Character)
local Player = game.Players:GetPlayerFromCharacter(Character
local UI = ReplicatedStorage:WaitForChild("UINAMEHERE"):Clone()
UI.Parent = Player.PlayerGui
end)
Put your UI Into ReplicatedStorage and name it something, and change in the script where it says âUINAMEHEREâ to whatever your UI is.
(I think I used âGetPlayerFromCharacterâ properly, if you get errors tell screen shot the output.)
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenDoor = ReplicatedStorage:WaitForChild("OpenDoor") -- RemoteEvent
UserInputService.InputBegan:Connect(function(InputObject, Processed)
if not Processed then
if InputObject.KeyCode == Enum.KeyCode.F then
OpenDoor:FireServer()
end
end
end)
ServerScript
--ServerScript Place this in your door with the clickdetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")
script.Parent.ClickDetector.MouseHoverEnter:connect(function(Player)
local UI = ReplicatedStorage:WaitForChild("Interaction"):Clone()
UI.Parent = Player.PlayerGui
end)
local OpenDoor = ReplicatedStorage:WaitForChild("OpenDoor") -- RemoteEvent
local speed = 4
local increment = 0
local direction = false -- false = left, true = right
local debounce = false
local door = script.Parent
OpenDoor.OnServerEvent:Connect(function()
if debounce == false then
print("boo")
debounce = true
increment = direction and 2 or -2
for i = 1,10 do
wait()
door.Orientation = door.Orientation + Vector3.new(0,increment,0) * speed
end
direction = not direction
debounce = false
end
end)
Change out your scripts to what I just posted here.
I tested them and they all work how they should.