Need Help Fixing an Old Gear Script

Good super-early morning everyone.
I’ve been working on redesigning an old gear, the Trololo Cape, for @Kiddie_Cannon and have reached a problem where the body doesn’t fully become transparent on the second attempt along with the handle. Here are some GIFs of the problem and the important part of the script:

https://gyazo.com/681800678cc6dfb0317dcc798b62e6e6.gif
As you can see in this first GIF, the handle becomes only partway transparent after it is used- which is very problematic.

https://gyazo.com/d641aa933c3b3cb73308e65e1fcae1c3.gif
Likewise, it also poses a problem when it’s clicked a second time. Remember, the torso and the handle are both halfway transparent.

https://gyazo.com/4dc69a7ad104eac8c7651c8f247f7523.gif
It gets even worse when I unequip, re-equip, and then click the gear. My body only becomes halfway transparent (as clarified by my zooming in).

What can I do to fix this problem? I appreciate any help that can be given to me.

Here’s the layout of the tool and both of the scripts:
https://gyazo.com/a0aec0236e491af47b8145017e9f6c79.png

ToolScript

-----------------
--| Constants |--
-----------------

local COOLDOWN = .1
local EFFECT_LENGTH = 2
local WALKSPEED_BOOST = 4
local INCREMENT = 0.1
local REGION_OFFSET = Vector3.new(15, 15, 15)

-----------------
--| Variables |--
-----------------

local Tool = script.Parent
local Handle = Tool:WaitForChild('Handle')

local WooshStart = Handle:WaitForChild('WooshStart')
local WooshEnd = Handle:WaitForChild('WooshEnd')
local ConfusionScript = script:WaitForChild('ConfusionScript')

local Character = nil
local Humanoid = nil
local Torso = nil

local Cape = nil
local Invisible = false
local HitList = {}

-----------------
--| Functions |--
-----------------

-- Returns all objects under instance with Transparency
local function GetTransparentsRecursive(instance, partsTable)
	local partsTable = partsTable or {}
	for _, child in pairs(instance:GetChildren()) do
		if child:IsA('BasePart') or child:IsA('Decal') then
			table.insert(partsTable, child)
		end
		GetTransparentsRecursive(child, partsTable)
	end
	return partsTable
end

-- Returns a character ancestor and its Humanoid, or nil
local function FindCharacterAncestor(subject)
	if subject and subject ~= Workspace then
		local humanoid = subject:FindFirstChild('Humanoid')
		if humanoid then
			return subject, humanoid
		else
			return FindCharacterAncestor(subject.Parent)
		end
	end
	return nil
end

local function ConfuseNearbyPlayers()
	HitList = {}
	local region3 = Region3.new(Torso.Position - REGION_OFFSET, Torso.Position + REGION_OFFSET)
	local parts = Workspace:FindPartsInRegion3(region3, Character, 100)
	for _, part in pairs(parts) do
		local character, humanoid  = FindCharacterAncestor(part)
		if character and humanoid and not HitList[character] then
			HitList[character] = true
			local confusionScriptClone = ConfusionScript:Clone()
			confusionScriptClone.Disabled = false
			confusionScriptClone.Parent = humanoid
		end
	end
end

local function ApplyEffect(yesPlease)
	local objectsWithTransparency = GetTransparentsRecursive(Character)
	repeat
		local lastObject = nil
		for _, object in pairs(objectsWithTransparency) do
			object.Transparency = object.Transparency + (yesPlease and INCREMENT or -INCREMENT)
			lastObject = object
		end
		wait()
	until (yesPlease and lastObject.Transparency >= 1 or lastObject.Transparency <= 0) or not lastObject
	Humanoid.WalkSpeed = Humanoid.WalkSpeed + (yesPlease and WALKSPEED_BOOST or -WALKSPEED_BOOST)
	local sound = yesPlease and WooshStart or WooshEnd
	sound:Play()
	if not yesPlease then
		ConfuseNearbyPlayers()
	end
	Invisible = yesPlease
end

local function OnEquipped()
	Character = Tool.Parent
	Humanoid = Character:WaitForChild('Humanoid')
	Torso = Character:WaitForChild('Torso')

	Spawn(function()
		Cape = Handle:Clone()
		Cape.Name = 'Cape'
		local weld = Instance.new('Weld')
		weld.Part0 = Torso
		weld.Part1 = Cape
		weld.C0 = CFrame.new(0, -.1, .6)
		weld.Parent = Cape
		Cape.Parent = Character
		Handle.Transparency = 1
	end)
end

local function OnActivated()
	if Tool.Enabled and Humanoid.Health 0 then
		Tool.Enabled = false

		ApplyEffect(true)
		wait(EFFECT_LENGTH)
		if Invisible then
			ApplyEffect(false)
		end

		wait(COOLDOWN)
		Tool.Enabled = true
	end
end

local function OnUnequipped()
	if Invisible then
		ApplyEffect(false)
	end
	Handle.Transparency = 0
	if Cape then
		Cape:Destroy()
		Cape = nil
	end
end

--------------------
--| Script Logic |--
--------------------

Tool.Equipped:connect(OnEquipped)
Tool.Activated:connect(OnActivated)
Tool.Unequipped:connect(OnUnequipped)

ConfusionScript

local Humanoid = script.Parent

Humanoid.WalkSpeed = Humanoid.WalkSpeed * -1

wait(2.5)

Humanoid.WalkSpeed = Humanoid.WalkSpeed * -1

script:Destroy()

Thanks in advance for all the help and support that you guys always give to me and the other developers out here. We really appreciate your usage of this platform to help us all succeed.

You could replace the repeat loop and use TweenService instead to change the transparencies. This would allow the tween to be interrupted and the set goal reached

1 Like

allow one more iteration so that it becomes fully invisible

1 Like

How would you recommend going about that? I’m new to scripting.

That sounds interesting. How would I go about doing that?

This is how you tween the transparency of one part, so just do this for each part in the character:

local TweenService = game:GetService("TweenService")
local goal = {}
goal.Transparency = 1
local tweenInfo = TweenInfo.new(3) -- 3 being how many seconds it takes to get to the transparency you set
local tween = TweenService:Create(part, tweenInfo, goal)
tween:Play()

@DutchDeveloper is rewriting the entire script. A big thank you to him.

1 Like