Part doesn't slowly fade

So I was making a LightSaber which you could turn on and off.

Unfourtunatly it is not working.

Here is the script

local Tool = script.Parent

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Saber = Tool:WaitForChild("Saber")

local showing = true

local SaberTab = {}

for i, v in pairs(Tool:GetChildren()) do
	if v.Name == "Saber" then
		table.insert(SaberTab, v)
	end
end

local function Saberfalse()
	
	if showing == true then
		for i = 1,0,0.1 do
			for _, v in pairs(SaberTab) do
				v.Transparency = 1
				showing = false
			end
		end
	end
end

local function SaberTrue()
	
	if showing == false then
		for i = 1,0,0.1 do
			for _, v in pairs(SaberTab) do
				v.Transparency = 0
				showing = true
			end
		end
	end
	
end

local Debounce = true

UIS.InputBegan:Connect(function(key)
	if Debounce then
		if key.KeyCode == Enum.KeyCode.E then
			Saberfalse()
			Debounce = false
		end
		
	elseif not Debounce then
		if key.KeyCode == Enum.KeyCode.E then
			Saberfalse()
			Debounce = true
		end
	end	
end)


There are no errors in the output.

Any help would be appriciated.

Can you show the saber tool in explorer?

Try switching this line

local Saber = Tool.Saber

to this one

local Saber = Tool:WaitForChild("Saber")

If this doesn’t work then you probably haven’t created a “Handle” part inside your tool or your tool parts aren’t welded

Yep that was the solution thank you soo much

No problem my dude! Good luck with your project!

Now I have another problem :sweat:

Can you help :grin:?

Alright, what’s your new issue?

The sword will slowly disappear if you click E on your Keyboard.
But it’s not working.

There are no errors in the output.

You have forgot to add

v.Transparency = v.Transparency + .1

And

  v.Transparency = v.Transparency -.1

Add this function into your script

local TS = game:GetService("TweenService")

function Disappear(Model)
 local TimeToDisappear = 3
 for _, Obj in next, Model:GetDescendants() do
  TS:Create(Obj, TweenInfo.new(TimeToDisappear, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Transparency = 1})
 end
end

and call it whenever you want the tool to disappear using:

Disappear(Tool)

Nope it isn’t working :frowning:

How would I add this to my script?
Can you tell?

Let me download Roblox Studio so I can format the code properly.

1 Like

Try this code:

local Tool = script.Parent

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Saber = Tool:WaitForChild("Saber")

local Showing = true

function ChangeTransparency(Model, Transparency)
	local TimeToDisappear = 3
	for _, Obj in next, Model:GetDescendants() do
		local Tween = TweenService:Create(Obj, TweenInfo.new(TimeToDisappear, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Transparency = Transparency})
		Tween:Play()
	end
end

local function SaberFalse()
	if Showing then
		ChangeTransparency(Tool, 1)
	end
end

local function SaberTrue()
	if not Showing then
		ChangeTransparency(Tool, 0)
	end
end

local Debounce = true

UIS.InputBegan:Connect(function(Input)
	if Debounce then
		if Input.KeyCode == Enum.KeyCode.E then
			SaberFalse()
			Debounce = false
		end
	elseif not Debounce then
		if Input.KeyCode == Enum.KeyCode.E then
			SaberTrue()
			Debounce = true
		end
	end	
end)

This is the script. (Hopefully)

 local Tool = script.Parent

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Saber = Tool:WaitForChild("Saber")

local showing = true

local SaberTab = {}

for i, v in pairs(Tool:GetChildren()) do
	if v.Name == "Saber" then
		table.insert(SaberTab, v)
	end
end

local function Saberfalse()

if showing == true then
	for i = 1,0,0.1 do
		for _, v in pairs(SaberTab) do
			v.Transparency = v.Transparency -.1
			showing = false
		end
	end
end
end

local function SaberTrue()

if showing == false then
	for i = 1,0,0.1 do
		for _, v in pairs(SaberTab) do
			v.Transparency = v.Transparency -.1
			showing = true
		end
	end
end

I repair your script, this should work

local Tool = script.Parent

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Saber = Tool:WaitForChild("Saber")

local showing = true

local SaberTab = {}

for i, v in pairs(Tool:GetChildren()) do
	if v.Name == "Saber" then
		table.insert(SaberTab, v)
	end
end

local function Saberfalse()
	if showing == true then
		for i = 0, 1, 0.1 do
			for _, v in pairs(SaberTab) do
				v.Transparency = i
			end
			wait(0.1)
		end
		showing = false
	end
end

local function SaberTrue()
	if showing == false then
		for i = 1, 0, -0.1 do
			for _, v in pairs(SaberTab) do
				v.Transparency = i
			end
			wait(0.1)
		end
		showing = true
	end
end

local Debounce = true
UserInputService.InputBegan:Connect(function (key)
	if Debounce then
		if key.KeyCode == Enum.KeyCode.E then
			Saberfalse()
			Debounce = false
		end
	elseif not Debounce then
		if key.KeyCode == Enum.KeyCode.E then
			SaberTrue()
			Debounce = true
		end
	end
end)
1 Like

Oh thank you soo much.

Sorry for late reply but what mistake did I make in my script?

  • the intention of the variable i in the loops seemed to be to control the transparency so I did v.Transparency = i
  • in SaberTrue() I did for i = 1, 0, -0.1 do so that i decreased
  • in Saberfalse() and SaberTrue() add wait(0.1) so that the transparency would be visible, without this that smooth transition would not be seen
  • in Saberfalse() and SaberTrue() the showing = false and showing = true should be outside the loops. This is just to not do extra work
  • SaberTrue() was not being used inside UserInputService.InputBegan:Connect(function (key)

those were the most important fixes

1 Like