So I have created a vault door that opens and closes when a player uses a keycard with a certain level, this also changes the readers light colour and also changes text on a screen.
Here is my code, it is a singe server script in the vault model:
local TS = game:GetService("TweenService")
local Info = TweenInfo.new(
5,
Enum.EasingStyle.Quad,
Enum.EasingDirection.Out,
0,
false,
0
)
local Hinge = script.Parent:WaitForChild("Hinge")
local Monitor = script.Parent:WaitForChild("Monitor").Screen.ScreenGui
local Reader = script.Parent:WaitForChild("Reader")
local DoorInfo =
{
Red = Color3.fromRGB(145, 0, 0),
Green = Color3.fromRGB(75, 151, 75),
Yellow = Color3.fromRGB(255, 255, 0),
OpenT = "Open",
ClosedT = "Closed",
OpeningT = "Opening...",
ClosingT = "Closing...",
Open = false,
CanUse = true,
}
local OpenDoor = TS:Create(
Hinge,
Info,
{CFrame = Hinge.CFrame * CFrame.Angles(0, math.rad(-115), 0)}
)
local CloseDoor = TS:Create(
Hinge,
Info,
{CFrame = Hinge.CFrame * CFrame.Angles(0, 0, 0)}
)
Reader.Body.Touched:Connect(function(Part)
if DoorInfo.CanUse and Part.Parent.Name == "Access Card" then
DoorInfo.CanUse = false
local Card = Part.Parent
if Card.ClearanceVal.Value >= 5 and not DoorInfo.Open then
DoorInfo.Open = true
Reader.Light.Color = DoorInfo.Green
Monitor.VaultStatus.Text = DoorInfo.OpeningT
Monitor.VaultStatus.TextColor3 = DoorInfo.Yellow
OpenDoor:Play()
wait(Info.Time + 2)
Reader.Light.Color = DoorInfo.Red
Monitor.VaultStatus.Text = DoorInfo.OpenT
Monitor.VaultStatus.TextColor3 = DoorInfo.Green
else
DoorInfo.Open = false
Reader.Light.Color = DoorInfo.Green
Monitor.VaultStatus.Text = DoorInfo.ClosingT
Monitor.VaultStatus.TextColor3 = DoorInfo.Yellow
OpenDoor:Play()
wait(Info.Time + 2)
Reader.Light.Color = DoorInfo.Red
Monitor.VaultStatus.Text = DoorInfo.ClosedT
Monitor.VaultStatus.TextColor3 = DoorInfo.Red
end
wait(5)
DoorInfo.CanUse = true
end
end)
This is the result:
Now I am wondering if there is any way to clean this up and point out if I am using any bad habits.
I mainly would like it to be shorter and use better methods.
Personally i hate tween service as it cant be used in conjunction with cframe and position changes and is more lengthly than lerping. But as for your use of tween service it seem to be a solid script. The only thing I have to say is actually to do with your modeling as it looks like a old manual vault but is actually electronic
The only thing id suggest is to move away from ts and look into lerping cframes in terms of scripting stuff. Aside from that some sounds and making the vault look more like modern electronic stuff would just finish it all off.
Thanks for the reply, I completely agree with you with the sounds and modelling. The modelling isn’t the best I was focusing on the scripting aspect of the game but I will try to work on making it more realistic and electronic. Also, I never considered using lerping for this and I am not completely sure how I would do it, would you mind giving me a starting point?
So lerping is usally the same for most thimgs and can be used for a ton of things.
function Lerp(a,b,c)
return (b - a) * c
end
a and b would be your two values that you are lerping between and c is a decimal of completion (1 means complete, so 0.5 would be 50% done)
So using that you can get any point along the path.
For example if i was to do this to a simple int value i would do something like:
local Completion = 0
local Number1 = 0
local Number2 = 1000000
while Completion < 1 do
print(Lerp(Number1, Number2,Completion))
Completion = Completion + 0.05
wait(0.05)
end
Im fairly sure for CFrames and positions your would lerp each of the values within it individually but id still chose it over a tween anyday
Prob cos your using roblox built in lerp, I dont get their lerp which is why I do it as my own function. Ill have a look on studio in 10mins and give you a hand if its still not going well. Going off memory atm
probably true, it doesn’t really matter that much, its only the fact that you can pull every number off of a lerp as well as perform multiple lerps at the same time that makes it better.
In terms of improvements, you have an if/else block that run almost the same code.
Try putting that in a function and fiddling with parameters to knock out a few lines of code.
In terms of tweens, I typically run visual stuff such as tweens and interpolations on the client; running it on the server has the potential to cause choppiness, but in most cases the choppiness is negligible.