Anyone got Better Scripting Tips

I am very New to scripting so sorry if the lines for script make you cringe…
But back to my question. I look at my code and it looks very messy. I would like some tips on how I can improve.

local Lid = script.Parent["Lid"]
local trail = script.Parent["Trail"]
local smoke = script.Parent["Smoke"]
local light = script.Parent["SurfaceLight"]
local screw = Lid.Screw
local heat = script.Parent["Heat"]
local crater = game.ReplicatedStorage.Crater

print("Shooting")
for i=1,75 do
	cylinder.CFrame=cylinder.CFrame+Vector3.new(100,-20,0)
	wait(0)
end

cylinder.Anchored = true
local newCrater = crater:Clone()
newCrater.CFrame = cylinder.CFrame
newCrater.Parent = workspace
cylinder.CanCollide = true
heat.Enabled = true
light.Enabled = false
trail.Enabled = false
smoke.Enabled = false
print("landed")
wait(40)
heat.Enabled = false
local weld = cylinder.WeldConstraint
screw.Enabled = true
Lid.Anchored = true
Lid.CanCollide = false
wait(2)
screw.Enabled = false
local rs: (RunService) = game:GetService("RunService")
local i = 0
while rs.Heartbeat:Wait() and i <= 500 do --100 is the amount of times you want the code below to run
	i += 1
	Lid.CFrame = Lid.CFrame * CFrame.Angles( 0, math.rad(1), 0 ) * CFrame.new(0,0.03,0) 
end
weld:Destroy()
Lid.Anchored = false
Lid.CanCollide = true
print("openLid")
2 Likes

Hi RealDrepyPlayzMC!

I like to space out different sections of my code using newlines, and use functions to make the readability easier. That being said, you should always try and write code so that if another developer were reading it without you, they’d have no problem understanding what everything does and such. I’ve attempted to re-write your code below with somewhat of what I would do!

local model = script.Parent;
local lid = model.Lid;
local trail = model.Trail;
local smoke = model.Smoke;
local light = model.SurfaceLight;
local screw = lid.Screw;
local heat = model.Heat;

local crater = game:GetService("ReplicatedStorage").Crater;
local runService = game:GetService("RunService");

local firstStageProcedure = function()
	for i = 1,75 do
		cylinder.CFrame = cylinder.CFrame + Vector3.new(100,-20,0)
	end
	
	local newCrater = crater:Clone();
	newCrater.Parent = game.Workspace;
	newCrater.CFrame = cylinder.CFrame;
	
	cylinder.Anchored = true;
	cylinder.CanCollide = true;
	
	heat.Enabled = true;
	light.Enabled, trail.Enabled, smoke.Enabled = false, false, false;
	print("Landed");
end

local secondStageProcedure = function()
	heat.Enabled = false;
	lid.CanCollide = false;
	screw.Enabled = true;
	lid.Anchored = true;
end

local finalStageProcedure = function()
	local weldConstraint = cylinder.WeldConstraint;
	screw.Enabled = false;

	local i = 0;
	while runService.Heartbeat:Wait() and i <= 500 do
		i += 1
		lid.CFrame = lid.CFrame * CFrame.Angles( 0, math.rad(1), 0 ) * CFrame.new(0,0.03,0) 
	end
	
	weldConstraint:Destroy()
	lid.Anchored = false
	lid.CanCollide = true
end

print("Shooting")

firstStageProcedure();
wait(40)

secondStageProcedure();
wait(2)

finalStageProcedure();

print("openLid")

4 Likes

I can tell you might possibly have some Java or C language background by your style.

1 Like

Haha, I do indeed have some C experience under my belt. :smiley: