Vault code review

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.

Thanks.

2 Likes

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 :thinking: :sweat_smile:

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.

Definatly a good start though

1 Like

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

Okay I get that a bit, but how would I re-arrange this to work for CFrame.Angles(), if that’s possible?

Lerp the angle, if you can put a number to it you can lerp it

1 Like

Okay thanks, let me try that real quick.

Might want to double check if its b-a or a-b tho, been a while since I do most my stuff thro modules i made a while ago

1 Like

I have no idea what I am doing wrong with this script:

local Part = script.Parent

for i = 0, 1, 0.05 do
	Part.CFrame = Part.CFrame * Part.CFrame:Lerp(CFrame.Angles(0, math.rad(90), 0), i)
	wait()
end

The part just vanishes.

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

1 Like

ok had a look, you cant just lerp the whole cframe as is, its best done by breaking it down into its x,y,z for the angles.

1 Like

I have no idea what I am doing, I am doing this:

local Part = script.Parent
wait(3)
for i = 0, 1, 0.05 do
	Part.CFrame:Lerp(CFrame.Angles(0, math.rad(90), 0), i)
	print(i)
	wait()
end
local CFs = {}

CFs[1] = game.Workspace.DevForumP.CFrame

CFs[2] = CFs[1] * CFrame.Angles(0,math.rad(90),0)

CFs[3] = 0

while CFs[3] < 1 do

game.Workspace.DevForumP.CFrame = CFs[1]:Lerp(CFs[2],CFs[3])

CFs[3] = CFs[3] + 0.025

wait(0.025)

end

Just played around w robloxs built in cframe lerp this works, so basically roblox made it easier to lerp cfs,

a:lerp(b,c)
CF1:lerp(CF2,Completion)
entire CFs not just the angles

Why can’t I use the built-in lerp?

you arent using a whole CF, you have to use the whole cf not just the angles

God this is so hard for me to understand… Tweening is so much simpler.

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.

1 Like

Are there any tutorials I can follow to understand it better?

This should help explain it, changed the link to the exact post that explains it simply

1 Like

Looks great!

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.

Overall, outstanding work!

1 Like