Is there any code that can do better then this?

This code is for a simulator game , if player collected a gems it will show +1 Gems Gui on screen .
is there any code that can do better then this ?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteFolder = ReplicatedStorage:WaitForChild("GemsGuiRemote")
local TweenService =  game:GetService("TweenService")

local Gems1 = script.Parent.Gems1
local Gems2 = script.Parent.Gems2
local Gems3 = script.Parent.Gems3
local count = script.Parent.Count


RemoteFolder.ShowLobbyGems.OnClientEvent:Connect(function()
	
	if count.Value == 1 then
		
		count.Value =2
		local CloneGems1 = Gems1:Clone()
		CloneGems1.Parent = script.Parent
		local CloneGems1TweenOff = TweenService:Create(
			CloneGems1,
			TweenInfo.new(.1),
			{
				ImageTransparency = 1
			}
		)
		local Indicator1 = TweenService:Create(
			CloneGems1.Indicator,
			TweenInfo.new(.1),
			{
				TextTransparency = 1
			}
		)
		CloneGems1.Indicator.Visible = true
		CloneGems1.Visible = true
		CloneGems1:TweenPosition(
			UDim2.new(0.682, 0,0.4, 0),
			Enum.EasingDirection.In,
			Enum.EasingStyle.Linear,
			true,
			2
		)
		wait(2)
		CloneGems1TweenOff:Play()
		Indicator1:Play()
		wait(.1)
		CloneGems1:Destroy()
		
	elseif count.Value == 2 then
		count.Value = 3
		local CloneGems2 = Gems2:Clone()
		CloneGems2.Parent = script.Parent
		local CloneGems2TweenOff = TweenService:Create(
			CloneGems2,
			TweenInfo.new(.1),
			{
				ImageTransparency = 1
			}
		)
		local Indicator2 = TweenService:Create(
			CloneGems2.Indicator,
			TweenInfo.new(.1),
			{
				TextTransparency = 1
			}
		)
		CloneGems2.Indicator.Visible = true
		CloneGems2.Visible = true
		CloneGems2:TweenPosition(
			UDim2.new(0.331, 0,0.2, 0),
			Enum.EasingDirection.In,
			Enum.EasingStyle.Linear,
			true,
			2
		)
		wait(2)
		CloneGems2TweenOff:Play()
		Indicator2:Play()
		wait(.1)
		CloneGems2:Destroy()
	elseif count.Value == 3 then
		count.Value = 1
		local CloneGems3 = Gems3:Clone()
		CloneGems3.Parent = script.Parent
		local CloneGems3TweenOff = TweenService:Create(
			CloneGems3,
			TweenInfo.new(.1),
			{
				ImageTransparency = 1
			}
		)
		local Indicator3 = TweenService:Create(
			CloneGems3.Indicator,
			TweenInfo.new(.1),
			{
				TextTransparency = 1
			}
		)
		CloneGems3.Indicator.Visible = true
		CloneGems3.Visible = true
		CloneGems3:TweenPosition(
			UDim2.new(0.377, 0,0.6, 0),
			Enum.EasingDirection.In,
			Enum.EasingStyle.Linear,
			true,
			2
		)
		wait(2)
		CloneGems3TweenOff:Play()
		Indicator3:Play()
		wait(.1)
		CloneGems3:Destroy()
	end
end)
1 Like

You could try add ModuleScripts to don’t make the same script again and again.

And you could add Gems inside a Folder.
Example:

local Folder = game.Workspace.**FolderName**:GetChildren()

for i = 1, #Folder do
   print(Folder[i])
end

Use a loop in a folder to detect if its a value that you want to change using IsA. Use more functions so your code is more organised and easier to understand whenever you look at it.
I didn’t see any datastore usage in your game, you need to save the amount of gems a player has with a datastore if you want it to reload the next time a player joins a server.

Example of neatness:

local variables = {"foo","baz","bork"}

local function UpdateGems(PlayerToUpdate)
    for i,v in pairs(PlayerToUpdate.GEMSFOLDER:GetChildren()) do
        if v:IsA("IntValue") then -- assuming its an integer based value
            v.Value = v.Value + 1
        end
    end
end

ReplicatedStorage.PlayerGotGem.OnServerEvent:Connect(function(player)
    UpdateGems(player)
end

-- alternatively you can just put the function itself in the :Connect()