Function going back to the first instead of the next click function

Hey y’all, I am having an issue with my script. It does work, but the problem is that it apparently after the first click, it works. But when I click it again, it goes back to the first function instead of the next one below.

  1. What do you want to achieve?
    I am trying to make the images slowly fade in and out, the fade in works, but not the fade out.

  2. What is the issue?
    It doesn’t go to the next code below.

  3. What solutions have you tried so far?
    I don’t know another solution as of right now

Here is the script:

function onClick()	
	
	local state = false
	local Screen1 = script.Parent.Parent.Parent.Parent.Parent.Parent.Screen1.Individual.ScreenDisplay.Camel
	local Screen2 = script.Parent.Parent.Parent.Parent.Parent.Parent.Screen2.Individual.ScreenDisplay.Camel2
	local Screen3 = script.Parent.Parent.Parent.Parent.Parent.Parent.Screen3.Individual.ScreenDisplay.Camel3
	local Screen4 = script.Parent.Parent.Parent.Parent.Parent.Parent.Screen4.Individual.ScreenDisplay.Camel4		
	local ts = game:GetService("TweenService")
	info = TweenInfo.new(0.5)
	
	
	local goal1 = {}
	goal1.ImageTransparency = 0

	local goal2 = {}
	goal2.ImageTransparency = 1
	
	
spawn(function()	
	state = not state
		if state then
			Screen1.ImageTransparency = 0.98
			Screen2.ImageTransparency = 0.98
			Screen3.ImageTransparency = 0.98
			Screen4.ImageTransparency = 0.98
			wait(0.5)
			script.Parent.BorderColor3 = Color3.new(255,0,0)
			ts:Create(Screen1,info,goal1):Play()
			ts:Create(Screen2,info,goal1):Play()
			ts:Create(Screen3,info,goal1):Play()
			ts:Create(Screen4,info,goal1):Play()
		else
			wait(0.5)
			script.Parent.BorderColor3 = Color3.new(0,170,0)
			ts:Create(Screen1,info,goal2):Play()
			ts:Create(Screen2,info,goal2):Play()
			ts:Create(Screen3,info,goal2):Play()
			ts:Create(Screen4,info,goal2):Play()
		end
	end)
end


script.Parent.MouseButton1Click:Connect(onClick)```

In cases like these it’s normally good to put variable declarations outside functions.

When calling onClick() it re-defines every variable every time you click, meaning state will always be set to false.

Here’s your edited script that fixes the issue:


local state = false
local Screen1 = script.Parent.Parent.Parent.Parent.Parent.Parent.Screen1.Individual.ScreenDisplay.Camel
local Screen2 = script.Parent.Parent.Parent.Parent.Parent.Parent.Screen2.Individual.ScreenDisplay.Camel2
local Screen3 = script.Parent.Parent.Parent.Parent.Parent.Parent.Screen3.Individual.ScreenDisplay.Camel3
local Screen4 = script.Parent.Parent.Parent.Parent.Parent.Parent.Screen4.Individual.ScreenDisplay.Camel4		
local ts = game:GetService("TweenService")
info = TweenInfo.new(0.5)
	
	
local goal1 = {}
goal1.ImageTransparency = 0

local goal2 = {}
goal2.ImageTransparency = 1

function onClick()
   spawn(function()	
  	 state = not state
	   if state then
		    Screen1.ImageTransparency = 0.98
			Screen2.ImageTransparency = 0.98
			Screen3.ImageTransparency = 0.98
			Screen4.ImageTransparency = 0.98
			wait(0.5)
			script.Parent.BorderColor3 = Color3.new(255,0,0)
			ts:Create(Screen1,info,goal1):Play()
			ts:Create(Screen2,info,goal1):Play()
			ts:Create(Screen3,info,goal1):Play()
			ts:Create(Screen4,info,goal1):Play()
		else
			wait(0.5)
			script.Parent.BorderColor3 = Color3.new(0,170,0)
			ts:Create(Screen1,info,goal2):Play()
			ts:Create(Screen2,info,goal2):Play()
			ts:Create(Screen3,info,goal2):Play()
			ts:Create(Screen4,info,goal2):Play()
		end
	end)
end

script.Parent.MouseButton1Click:Connect(onClick)
2 Likes

Oh gosh! I completely forgot about the spawn:() function, it works now. Thanks in advance!

1 Like