[Before VS After] Difference in coding after 1,5 years

Please consider using the “What are you working on currently?” topic!

So basically I am having a playground game where I come back from time to time and use my knowledge to see what I can do better. It’s quite fun to compare older scripts to newer ones.

A ‘handler’ for bamboo that can be cut with a sickle
Went from ~223 Lines of code to ~75 Lines

Example video of what both the scripts do


Old code

local health = 5
local maxhealth = 5
local Cutter = script.Parent.Cutter
local Bamboo = script.Parent.Parent.ObereParts
local BillBoard = Bamboo.Anzeige.BillboardGui
local TextLabel = BillBoard.TextLabel
local ableToCut = true
local ServerStorage = game:GetService("ServerStorage")
local PlayerData = ServerStorage:FindFirstChild("PlayerData")

local BambooStats = {
	Oben1 = {
		Position = {X = 0,Y = 0,Z = 0},
		Orientation = {X = 0,Y = 0,Z = 0}
	},

	Oben2 = {
		Position = {X = 0,Y = 0,Z = 0},
		Orientation = {X = 0,Y = 0,Z = 0}
	},

	Oben3 = {
		Position = {X = 0,Y = 0,Z = 0},
		Orientation = {X = 0,Y = 0,Z = 0}
	},

	Oben4 = {
		Position = {X = 0,Y = 0,Z = 0},
		Orientation = {X = 0,Y = 0,Z = 0}
	},

	Oben5 = {
		Position = {X = 0,Y = 0,Z = 0},
		Orientation = {X = 0,Y = 0,Z = 0}
	},

	Oben6 = {
		Position = {X = 0,Y = 0,Z = 0},
		Orientation = {X = 0,Y = 0,Z = 0}
	},

	Oben7 = {
		Position = {X = 0,Y = 0,Z = 0},
		Orientation = {X = 0,Y = 0,Z = 0}
	},

	Oben8 = {
		Position = {X = 0,Y = 0,Z = 0},
		Orientation = {X = 0,Y = 0,Z = 0}
	},

	Anzeige = {
		Position = {X = 0,Y = 0,Z = 0},
		Orientation = {X = 0,Y = 0,Z = 0}
	},


	BambooRoot = { 
		Position = {X = 0,Y = 0,Z = 0 },
		Orientation = {X = 0,Y = 0,Z = 0 },
	},

}


function resetposition(plr)
	wait(8)
	for i, v in pairs(Bamboo:GetChildren()) do
		v.Anchored = true
	end

	for i,v in pairs(Bamboo.Root:GetChildren()) do
		if v:IsA("WeldConstraint") then
			v.Enabled = false
		end
	end
	Bamboo.Root.Anchored = true
	for i, v in pairs(BambooStats) do
		for a, b in pairs(Bamboo:GetChildren()) do
		end
	end
	Bamboo.Root.Position = Vector3.new(BambooStats.BambooRoot.Position.X,BambooStats.BambooRoot.Position.Y,BambooStats.BambooRoot.Position.Z)
	Bamboo.Root.Orientation = Vector3.new(BambooStats.BambooRoot.Orientation.X,BambooStats.BambooRoot.Orientation.Y,BambooStats.BambooRoot.Orientation.Z)
	Bamboo.Oben1.Position = Vector3.new(BambooStats.Oben1.Position.X,BambooStats.Oben1.Position.Y,BambooStats.Oben1.Position.Z)
	Bamboo.Oben1.Orientation = Vector3.new(BambooStats.Oben1.Orientation.X,BambooStats.Oben1.Orientation.Y,BambooStats.Oben1.Orientation.Z)
	Bamboo.Oben2.Position = Vector3.new(BambooStats.Oben2.Position.X,BambooStats.Oben2.Position.Y,BambooStats.Oben2.Position.Z)
	Bamboo.Oben2.Orientation = Vector3.new(BambooStats.Oben2.Orientation.X,BambooStats.Oben2.Orientation.Y,BambooStats.Oben2.Orientation.Z)
	Bamboo.Oben3.Position = Vector3.new(BambooStats.Oben3.Position.X,BambooStats.Oben3.Position.Y,BambooStats.Oben3.Position.Z)
	Bamboo.Oben3.Orientation = Vector3.new(BambooStats.Oben3.Orientation.X,BambooStats.Oben3.Orientation.Y,BambooStats.Oben3.Orientation.Z)
	Bamboo.Oben4.Position = Vector3.new(BambooStats.Oben4.Position.X,BambooStats.Oben4.Position.Y,BambooStats.Oben4.Position.Z)
	Bamboo.Oben4.Orientation = Vector3.new(BambooStats.Oben4.Orientation.X,BambooStats.Oben4.Orientation.Y,BambooStats.Oben4.Orientation.Z)
	Bamboo.Oben5.Position = Vector3.new(BambooStats.Oben5.Position.X,BambooStats.Oben5.Position.Y,BambooStats.Oben5.Position.Z)
	Bamboo.Oben5.Orientation = Vector3.new(BambooStats.Oben5.Orientation.X,BambooStats.Oben5.Orientation.Y,BambooStats.Oben5.Orientation.Z)
	Bamboo.Oben6.Position = Vector3.new(BambooStats.Oben6.Position.X,BambooStats.Oben6.Position.Y,BambooStats.Oben6.Position.Z)
	Bamboo.Oben6.Orientation = Vector3.new(BambooStats.Oben6.Orientation.X,BambooStats.Oben6.Orientation.Y,BambooStats.Oben6.Orientation.Z)
	Bamboo.Oben7.Position = Vector3.new(BambooStats.Oben7.Position.X,BambooStats.Oben7.Position.Y,BambooStats.Oben7.Position.Z)
	Bamboo.Oben7.Orientation = Vector3.new(BambooStats.Oben7.Orientation.X,BambooStats.Oben7.Orientation.Y,BambooStats.Oben7.Orientation.Z)
	Bamboo.Oben8.Position = Vector3.new(BambooStats.Oben8.Position.X,BambooStats.Oben8.Position.Y,BambooStats.Oben8.Position.Z)
	Bamboo.Oben8.Orientation = Vector3.new(BambooStats.Oben8.Orientation.X,BambooStats.Oben8.Orientation.Y,BambooStats.Oben8.Orientation.Z)
	Bamboo.Anzeige.Position = Vector3.new(BambooStats.Anzeige.Position.X,BambooStats.Anzeige.Position.Y,BambooStats.Anzeige.Position.Z)
	Bamboo.Anzeige.Orientation = Vector3.new(BambooStats.Anzeige.Orientation.X,BambooStats.Anzeige.Orientation.Y,BambooStats.Anzeige.Orientation.Z)
	for t,h in pairs(Bamboo.Root:GetChildren()) do
		if h:IsA("WeldConstraint") then
			h.Enabled = true
		end
	end
	for y,x in pairs(Bamboo:GetChildren()) do
		if x.Name ~= "Root" then
			x.Anchored = false
		end
		--print("Parts have been unanchored")
	end
	wait(2)
	print("Health: "..health,ableToCut,Cutter.Value)
	wait(1)
	Cutter.Value = "None"
	health = maxhealth
	TextLabel.Text = "5/5"
	ableToCut = true
	print("Health: "..health,ableToCut,Cutter.Value)
	if game.Players:FindFirstChild(plr) then
		game.Players:FindFirstChild(plr):WaitForChild("PlayerStats"):FindFirstChild("CanCut").Value = true
	end
	print("Health: "..health,ableToCut,Cutter.Value)
end



for i, v in pairs(Bamboo:GetChildren()) do
	if v:IsA("MeshPart") then
		v.Touched:Connect(function(hit)
			if hit.Name == "Klinge" then
				if Cutter.Value == "None" or hit.Parent.Parent.Parent.Name == Cutter.Value then
					if game.Players:FindFirstChild(hit.Parent.Parent.Parent.Name).PlayerStats.isCuting.Value == true and PlayerData:FindFirstChild(hit.Parent.Parent.Parent.Name):FindFirstChild("Bamboo").Value < PlayerData:FindFirstChild(hit.Parent.Parent.Parent.Name):FindFirstChild("Max Bamboo").Value then
						if game.Players:FindFirstChild(hit.Parent.Parent.Parent.Name).PlayerStats.CanCut.Value == true then game.Players:FindFirstChild(hit.Parent.Parent.Parent.Name).PlayerStats.CanCut.Value = false
							if health ~= 0 and ableToCut == true then
								--print("Player: "..hit.Parent.Parent.Parent.Name," Health:",health,"Max Health: ",maxhealth,"AbleToCut: ",ableToCut," Cutter: ",Cutter.Value,"Players Collected Bamboo: ",PlayerData:FindFirstChild(hit.Parent.Parent.Parent.Name):FindFirstChild("Bamboo").Value,"Players Max Storage Amount",PlayerData:FindFirstChild(hit.Parent.Parent.Parent.Name):FindFirstChild("Max Bamboo").Value)
								--print(hit.Parent.Parent.Parent.Name,PlayerData:FindFirstChild(hit.Parent.Parent.Parent.Name):FindFirstChild("Bamboo").Value,PlayerData:FindFirstChild(hit.Parent.Parent.Parent.Name):FindFirstChild("Max Bamboo").Value)		
								if Cutter.Value == "None" then
									Cutter.Value = game.Players:FindFirstChild(hit.Parent.Parent.Parent.Name).Name
								end
								health = health - 2.5
								TextLabel.Text = "Cutter: "..hit.Parent.Parent.Parent.Name.." "..health.."/"..maxhealth
								if health <= 0 then
									TextLabel.Text = "0/5"
									ableToCut = false
									PlayerData:FindFirstChild(Cutter.Value):FindFirstChild("Bamboo").Value = PlayerData:FindFirstChild(Cutter.Value):FindFirstChild("Bamboo").Value + 1
									PlayerData:FindFirstChild(Cutter.Value):FindFirstChild("Most Bamboo").Value = PlayerData:FindFirstChild(Cutter.Value):FindFirstChild("Most Bamboo").Value + 1
									game.Players:FindFirstChild(Cutter.Value):FindFirstChild("leaderstats").MBamboo.Value = game.Players:FindFirstChild(Cutter.Value):FindFirstChild("leaderstats").MBamboo.Value + 1
									BambooStats.BambooRoot.Position.X = Bamboo.Root.Position.X
									BambooStats.BambooRoot.Position.Y = Bamboo.Root.Position.Y
									BambooStats.BambooRoot.Position.Z = Bamboo.Root.Position.Z
									----------------------------------------------------------
									BambooStats.BambooRoot.Orientation.X = Bamboo.Root.Orientation.X
									BambooStats.BambooRoot.Orientation.Y = Bamboo.Root.Orientation.Y
									BambooStats.BambooRoot.Orientation.Z = Bamboo.Root.Orientation.Z
									----------------------------------------------------------------
									BambooStats.Oben1.Position.X = Bamboo.Oben1.Position.X
									BambooStats.Oben1.Position.Y = Bamboo.Oben1.Position.Y
									BambooStats.Oben1.Position.Z = Bamboo.Oben1.Position.Z
									------------------------------------------------------
									BambooStats.Oben1.Orientation.X = Bamboo.Oben1.Orientation.X
									BambooStats.Oben1.Orientation.Y = Bamboo.Oben1.Orientation.Y
									BambooStats.Oben1.Orientation.Z = Bamboo.Oben1.Orientation.Z	
									------------------------------------------------------
									BambooStats.Oben2.Position.X = Bamboo.Oben2.Position.X
									BambooStats.Oben2.Position.Y = Bamboo.Oben2.Position.Y
									BambooStats.Oben2.Position.Z = Bamboo.Oben2.Position.Z
									------------------------------------------------------
									BambooStats.Oben2.Orientation.X = Bamboo.Oben2.Orientation.X
									BambooStats.Oben2.Orientation.Y = Bamboo.Oben2.Orientation.Y
									BambooStats.Oben2.Orientation.Z = Bamboo.Oben2.Orientation.Z
									------------------------------------------------------		
									BambooStats.Oben3.Position.X = Bamboo.Oben3.Position.X
									BambooStats.Oben3.Position.Y = Bamboo.Oben3.Position.Y
									BambooStats.Oben3.Position.Z = Bamboo.Oben3.Position.Z
									------------------------------------------------------
									BambooStats.Oben4.Position.X = Bamboo.Oben4.Position.X
									BambooStats.Oben4.Position.Y = Bamboo.Oben4.Position.Y
									BambooStats.Oben4.Position.Z = Bamboo.Oben4.Position.Z
									------------------------------------------------------
									BambooStats.Oben5.Position.X = Bamboo.Oben5.Position.X
									BambooStats.Oben5.Position.Y = Bamboo.Oben5.Position.Y
									BambooStats.Oben5.Position.Z = Bamboo.Oben5.Position.Z
									------------------------------------------------------
									BambooStats.Oben6.Position.X = Bamboo.Oben6.Position.X
									BambooStats.Oben6.Position.Y = Bamboo.Oben6.Position.Y
									BambooStats.Oben6.Position.Z = Bamboo.Oben6.Position.Z
									------------------------------------------------------
									BambooStats.Oben7.Position.X = Bamboo.Oben7.Position.X
									BambooStats.Oben7.Position.Y = Bamboo.Oben7.Position.Y
									BambooStats.Oben7.Position.Z = Bamboo.Oben7.Position.Z
									------------------------------------------------------
									BambooStats.Oben8.Position.X = Bamboo.Oben8.Position.X
									BambooStats.Oben8.Position.Y = Bamboo.Oben8.Position.Y
									BambooStats.Oben8.Position.Z = Bamboo.Oben8.Position.Z
									------------------------------------------------------
									BambooStats.Anzeige.Position.X = Bamboo.Anzeige.Position.X
									BambooStats.Anzeige.Position.Y = Bamboo.Anzeige.Position.Y
									BambooStats.Anzeige.Position.Z = Bamboo.Anzeige.Position.Z
									------------------------------------------------------
									Bamboo.Root.Anchored = false
									Bamboo.Root.Velocity = Vector3.new(3,3,0)
									game.Players:FindFirstChild(Cutter.Value):FindFirstChild("leaderstats").Bamboo.Value = game.Players:FindFirstChild(Cutter.Value):FindFirstChild("leaderstats").Bamboo.Value + 1
									wait(0.5)
									if game.Players:FindFirstChild(Cutter.Value) then
										game.Players:FindFirstChild(Cutter.Value):WaitForChild("PlayerStats"):FindFirstChild("CanCut").Value = true
									end
									resetposition(Cutter.Value)
									--	print("Health: "..health)
								end
								wait(1)
								if game.Players:FindFirstChild(Cutter.Value) then
									game.Players:FindFirstChild(Cutter.Value):WaitForChild("PlayerStats"):FindFirstChild("CanCut").Value = true
								end
							end
						end
					end
				end
			end
		end)
	end	
end

As of recently :arrow_down: [Not fully done]

local bamboo = {}
bamboo.__index = bamboo

local storage = game:GetService("ServerStorage")

local util = storage.Utilities
local getBaseParts = require(util.getBaseParts)
local getPlayer = require(util.getPlayer)

function bamboo:new(instance)
	return setmetatable({
		["bamboo"] = instance,
		["cutter"] = "",
		["canHit"] = true,
		["health"] = {5, 5},
		["origin"] = (function()
			local c = {}
			for _, part in pairs(getBaseParts(instance)) do
				c[part] = {part.Position, part.Orientation}
			end return c
		end)(),
	}, self)
end

function bamboo.reset(self)
	self.bamboo.Upper.Root.Anchored = true
	
	for _, part in pairs(getBaseParts(self.bamboo)) do
		part.Position = self.origin[part][1]
		part.Orientation = self.origin[part][2]
	end
	
	self.cutter = ""
	self.health = {self.health[2], self.health[2]}
	self.canHit = true
end

function bamboo.detach(self)
	self.bamboo.Upper.Root.Anchored = false
	self.bamboo.Upper.Root.AssemblyLinearVelocity = Vector3.new(
		math.random(-5,5),
		25,
		math.random(-5,5)
	)
	
	task.wait(5)
	
	bamboo.reset(self)
end

function bamboo:onTouch(hit, env)
	if (self.canHit == false) then return end
	if (hit.Name ~= "Klinge") then return end

	local player = getPlayer(hit)
	local env = player.Character:FindFirstChild("getEnv", true):Invoke()

	if (self.cutter == "" or self.cutter == player.Name) then
		if env.swinging == false then return end

		self.canHit = false
		self.cutter = player.Name
		self.health[1] -= 1

		if (self.health[1] <= 0) then
			bamboo.detach(self)
			return
		end

		task.wait(1)
		self.canHit = true
	end
end

return bamboo
4 Likes