Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I script a toggle on and off button for trails?

How do I script a code to toggle on and off trails when clicked on? I have been trying to add one for months but just can not figure it out.

1 comment

0

Step 1) Create a trail in your character. Step 2) Create a button. Step 3) Listen for when the button is pressed, with an event. Step 4) Change the enabled property to the opposite of it current property.

1 answer

✓ Accepted answer

Just developed a bit of code that can be run in a Local script, it creates all the necessary things to turn on/off a trail. oh and it also creates the Gui and the trail plus parts required.

Oh did i mention it also moves the trail just infront/around the players camera? it does that too! lol

as you see i got a little carried away lol oh well, onto the script.

Must be a LocalScript and put inside your StarterPack folder!

local Camera = workspace.CurrentCamera

--[[
Note this script creates a part and a trail inside And some attachment points for the trail
The script then makes a Gui TextButton inside the players PlayerGui folder and hooks up the 
Button to switch on/off the trail

The last bit is moving the part in a kinda random motion to show the trail

Not sure if this script will work online as in other players seeing the created parts other than the 
player that runs this script so it should ONLY run from within the studio But the button script should 
work online!

]]


--// a function for creating a part with a rainbow trail inside
function createBit()
	local Bit = Instance.new("Part",workspace)
	local Trail = Instance.new("Trail",Bit)
	local Att_A = Instance.new("Attachment",Bit)
	local Att_B = Instance.new("Attachment",Bit)

	Trail.Attachment0 = Att_A
	Trail.Attachment1 = Att_B
	Trail.Color = ColorSequence.new({
		ColorSequenceKeypoint.new(0, Color3.fromRGB(0,0,0)),
		ColorSequenceKeypoint.new(0.1, Color3.fromRGB(255,255,255)),
		ColorSequenceKeypoint.new(0.2, Color3.fromRGB(255,140,0)),
		ColorSequenceKeypoint.new(0.3, Color3.fromRGB(194,94,255)),
		ColorSequenceKeypoint.new(0.4, Color3.fromRGB(255,255,0)),
		ColorSequenceKeypoint.new(0.5, Color3.fromRGB(0,255,0)),
		ColorSequenceKeypoint.new(0.6, Color3.fromRGB(0,255,255)),
		ColorSequenceKeypoint.new(0.7, Color3.fromRGB(0,0,255)),
		ColorSequenceKeypoint.new( 0.8, Color3.fromRGB(255,0,255)),
		ColorSequenceKeypoint.new(0.9, Color3.fromRGB(255,0,0)),
		ColorSequenceKeypoint.new(1, Color3.fromRGB(0,0,0))
	})
	Trail.Transparency = NumberSequence.new({
		NumberSequenceKeypoint.new(0,1),
		NumberSequenceKeypoint.new(0.1,0),
		NumberSequenceKeypoint.new(0.9,0),
		NumberSequenceKeypoint.new(1,1),
	})
	Trail.LightEmission = 0.5
	Trail.LightInfluence = 0
	Att_A.Position = Vector3.new(0,2,0)
	Att_B.Position = Vector3.new(0,-2,0)
	Bit.Transparency = 1
	Bit.Anchored = true
	Bit.CanCollide = false
	Bit.Locked = true
	return Bit
end
--[[ Some values for moving our slave part this is just to randomize(kinda) 
the movement of the trail so we can see it!
]]
local Radius = 30
local X = 0
local Y = 0 

local t = 0
local u = 0

local TrailEnabled = false
--[[ 
Local Gui Creation Bit!
]]

local PlayerGui = script.Parent.Parent:WaitForChild("PlayerGui",10)
if(PlayerGui == nil) then error("Error: Gui Couldn't be found in ["..script.Parent.Parent.Name.."]! \n "..debug.traceback()) end

local Gui = Instance.new("ScreenGui",PlayerGui)
Gui.Name = "TrailGui Control"
local Button = Instance.new("TextButton",Gui)
Button.Active = true
Button.AnchorPoint = Vector2.new(0,0)
Button.AutoButtonColor = true
Button.BackgroundColor3 = Color3.fromRGB(255,255,255)
Button.BackgroundTransparency = 0
Button.BorderColor3 = Color3.fromRGB(27,42,53)
Button.BorderMode = Enum.BorderMode.Outline
Button.BorderSizePixel = 1
Button.LayoutOrder = 0
Button.Name = "Awesome Trail Gui Button!"
Button.Size = UDim2.new(0,200,0,50)
Button.Position = UDim2.new(0, 100,0,20)
Button.Text = "Click For Trail!"

local ButtonDefault_Text = "Click For an Awesome Trail!"
local ButtonHideTrail_Text = "Click to Hide Trail *cries*"

--[[ the important bit for turning on/off the trail! ]]
Button.MouseButton1Click:Connect(function()
	TrailEnabled = not TrailEnabled -- toggle our TrailEnabled to true/false
	-- this bit simply toggles the button text from ButtonDefault_Text when the trails not enabled to ButtonHideTrail_Text when it is!
	Button.Text = TrailEnabled == true and ButtonHideTrail_Text or ButtonDefault_Text
end)
	--// call our creator function ...... Hai God!
local RainbowTrailPart = createBit()


--// main loop for simply moving our dummy/slave part around and to toggle the visiblility of the trail
while true do
	--// math stuff
	local AmpA = t / (math.pi * 180)
	local AmpB = u / (math.pi * 225)
	--// more math stuff
	local X = Radius* math.cos(AmpA)* math.cos(AmpB)
	local Z = Radius* math.sin(AmpA)* math.sin(AmpB)
	--// set the cframe of our created dummy part to the camera of the player but with an offset so it shows just infront of the camera
	RainbowTrailPart.CFrame = Camera.CFrame * CFrame.new(X,-3,Z-35)
	
	--// find the trail inside our created dummy part
	local Trail = RainbowTrailPart:FindFirstChild("Trail")
	
	--// if we find it then set it to what ever TrailEnabled is set to
	if(Trail ~= nil) then
		Trail.Enabled = TrailEnabled
	end
	
	--// more math stuff ;P
	t = t + 120
	u = u + 30
	wait()
end

--[[
 Note:
 the math stuff is simply a glorified circle
 i encourage yoiu to play around with the values but not before backing up first! :)
]]

hope this helps! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.