I am trying to create a tycoon merging game and what this code does is whenever you touch a button
and you have three dummies of the same type you merge themand get a better one, the only problem is that the code is very infficient.
local Button = script.Parent
local NormalAvatar = 0
local BlueAvatar = 0
local Avatars = {}
local NumberOfAvatars = 0
local debounce = false
local Index = 1
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
Button.Touched:Connect(function(hit)
if hit.Parent:WaitForChild("Humanoid") and debounce == false then
debounce = true
for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
if v.Name == "NormalAvatar" then
NormalAvatar += 1
elseif v.Name == "BlueAvatar" then
BlueAvatar += 1
end
end
if NormalAvatar > 3 and NormalAvatar % 3 ~= 0 then
repeat NormalAvatar -= 1 until NormalAvatar % 3 == 0
end
if BlueAvatar > 3 and BlueAvatar % 3 ~= 0 then
repeat BlueAvatar -= 1 until BlueAvatar % 3 == 0
end
if NormalAvatar >= 3 then
for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
if NormalAvatar == 0 then continue end
if v.Name == "NormalAvatar" then
v:Destroy()
NormalAvatar -= 1
if i % 3 == 0 then
local BlueAvatar = ReplicatedStorage.Avatars.BlueAvatar:Clone()
BlueAvatar.Parent = game.Workspace.Tycoons.Tycoon1.Avatars
for i ,v in pairs(BlueAvatar:GetDescendants()) do
if v:IsA("Part") or v:IsA("MeshPart") then
if v.Name ~= "HumanoidRootPart" then
v.Transparency = 1
local Tween = TweenService:Create(v,TweenInfo.new(.5,Enum.EasingStyle.Linear,Enum.EasingDirection.In),{Transparency = 0 })
Tween:Play()
end
end
end
end
end
end
for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
table.insert(Avatars , v)
end
NumberOfAvatars = #Avatars
for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
if i == 1 then
v.HumanoidRootPart.CFrame = CFrame.new(320, -28.712 , 117)
elseif i % 5 == 0 and i == NumberOfAvatars then
v.HumanoidRootPart.CFrame = CFrame.new(320, Avatars[Index].HumanoidRootPart.CFrame.Y + 9.288 , 117)
Index = i
elseif i < 5 then
v.HumanoidRootPart.CFrame = CFrame.new(Avatars[i - 1].HumanoidRootPart.CFrame.X - 12, Avatars[1].HumanoidRootPart.CFrame.Y , 117)
elseif i >= 5 and i ~= Index then
v.HumanoidRootPart.CFrame = CFrame.new(Avatars[i - 1].HumanoidRootPart.CFrame.X - 12, Avatars[Index].HumanoidRootPart.CFrame.Y , 117)
end
end
end
for i = #Avatars , 1 , -1 do
table.remove(Avatars,i)
end
NumberOfAvatars = 0
NormalAvatar = 0
BlueAvatar = 0
wait(1)
debounce = false
end
end)