Script only works first time

why does my script only work on the first time?

local Button = script.Parent.Button


local TS = game:GetService("TweenService")
local Info = TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
local state = false


function onClick1()

	
	if state == false then 
		local state = true
		TS:Create(Button, Info, {BackgroundColor3 = Color3.new(1, 1, 1)}):Play()
		
	else 	
		local state = false
		TS:Create(Button, Info, {BackgroundColor3 = Color3.new(0.141176, 0.141176, 0.141176)}):Play()
	end
	
	
end
script.Parent.Button.MouseButton1Click:Connect(onClick1)

In the function you are setting state = true locally, therefore the actual state variable does not change outside the function and remains false every time you run the function.
You can fix this by changing local state = true to state = true and local state = false to state = false.
Also as a side note: you might find it easier to use Color3.fromRGB() rather than Color3.new().

1 Like