Cleaning Up Sloppy TweenService Code

I’m making some random crappy game just to improve my coding skills, and I would appreciate if anyone could give me some best practice tips on my script. Every three seconds this script spawns in three random blocks at random locations and tweens the colors. A lot of the code seems to be somewhat redundant.

local TweenService = game:GetService("TweenService")


local Game1 = game.ReplicatedStorage.G1
local StartButton1 = game.Workspace.StartG1
local Timer = game.ReplicatedStorage.G1RoundTimer
local DanceFloor = game.Workspace.Stage1.DeathFloor

local fx = math.random(-101,-5)
local fz = math.random(-47,51)
local x2 = math.random(-101,-5)
local z2 = math.random(-47,51)
local x3 = math.random(-101,-5)
local z3 = math.random(-47,51)

local fart = Instance.new("Part",workspace)
fart.CFrame = CFrame.new(fx,2,fz)
fart.Parent = nil
fart.BrickColor = BrickColor.Red()
fart.Anchored = true
fart.Size = Vector3.new(10,1,10)
fart.Material = ("Neon")

local fart2 = Instance.new("Part",workspace)
fart2.CFrame = CFrame.new(x2,2,z2)
fart2.Parent = nil
fart2.BrickColor = BrickColor.Red()
fart2.Anchored = true
fart2.Size = Vector3.new(10,1,10)
fart2.Material = ("Neon")

local fart3 = Instance.new("Part",workspace)
fart3.CFrame = CFrame.new(x3,2,z3)
fart3.Parent = nil
fart3.BrickColor = BrickColor.Red()
fart3.Anchored = true
fart3.Size = Vector3.new(10,1,10)
fart3.Material = ("Neon")

local info = TweenInfo.new(.5)
local goal = {}
goal.Color = Color3.fromRGB(0,0,0)


function glowTween()
	local Tween = TweenService:Create(fart, info, goal)
	Tween:Play()
	local Tween2 = TweenService:Create(fart2, info, goal)
	Tween2:Play()
	local Tween3 = TweenService:Create(fart3, info, goal)
	Tween3:Play()
	wait(.5)
	goal.Color = Color3.fromRGB(255,0,0)
	local Tween = TweenService:Create(fart, info, goal)
	local Tween2 = TweenService:Create(fart2, info, goal)
	local Tween3 = TweenService:Create(fart3, info, goal)
	Tween3:Play()
	Tween2:Play()
	Tween:Play()
	wait(.5)
	goal.Color = Color3.fromRGB(0,0,0)
	local Tween = TweenService:Create(fart, info, goal)
	local Tween2 = TweenService:Create(fart2, info, goal)
	local Tween3 = TweenService:Create(fart3, info, goal)
	Tween3:Play()
	Tween2:Play()
	Tween:Play()
	wait(.5)
	goal.Color = Color3.fromRGB(255,0,0)
	local Tween = TweenService:Create(fart, info, goal)
	local Tween2 = TweenService:Create(fart2, info, goal)
	local Tween3 = TweenService:Create(fart3, info, goal)
	Tween3:Play()
	Tween2:Play()
	Tween:Play()
	wait(.5)
	goal.Color = Color3.fromRGB(0,0,0)
end

function play()
	if Game1.Value == 1 then
		repeat
			DanceFloor.Parent = workspace.Stage1
			fart.Parent = workspace
			fart2.Parent = workspace
			fart3.Parent = workspace
			fz = math.random(-47,51)
			fx = math.random(-101,-5)
			x2 = math.random(-101,-5)
			z2 = math.random(-47,51)
			x3 = math.random(-101,-5)
			z3 = math.random(-47,51)

			fart.CFrame = CFrame.new(fx,2,fz)
			fart2.CFrame = CFrame.new(x2,2,z2)
			fart3.CFrame = CFrame.new(x3,2,z3)
			glowTween()
			DanceFloor.Parent = nil
			wait(3)
		until Game1.Value == 0
	end
	if Game1.Value == 0 then
		fart.Parent  = nil
		fart2.Parent = nil
		fart3.Parent = nil
		Timer.Value = 10
		StartButton1.Parent = game.Workspace
		
	end
end


Game1:GetPropertyChangedSignal("Value"):Connect(play)

here is the code in action if it matters

  1. Name your variables properly.
  2. Don’t set the parent in the instance function, set it after you have set all the other properties. It probably won’t make much of a difference here, but it is much better for performance.
  3. You’re creating a bunch of similar tweens instead of just reusing them.
  4. You have a lot of repeating code. Use for loops or functions.
2 Likes

I thought I was naming variables properly?
Thank you.

1 Like

this sticks out to me the most

1 Like