How to move GUI every second

Hello everybody, so I have a flashlight and a GUI meter. I want that meter to to the right when player’s flashlight is off, and go left when the flashlight if on. The issue is I don’t know how to move GUIs every second little to the right or left. You could send me a page or a youtube tutorial. Huge thanks for helping!

Edit: I kinda did what I wanted. Here’s the script:

local flashlight = game:GetService("Players").LocalPlayer.Character:WaitForChild("Head").PlayerLightAttachment[game:GetService("Players").LocalPlayer.Name.."'s Flashlight"]
local darkness = 0

local function getDarkness ()
	darkness += 1
end

local function loseDarkness ()
	if darkness > 0 then
		darkness -= 1
	end
end

while wait(0.1) do
	script.Parent:TweenPosition(UDim2.new(-1, darkness, 0,0), "Out", "Quad", 0)
	if flashlight.Enabled == false then
		getDarkness()
	else
		loseDarkness()
	end
end

I made a new variable “dakness” the more darkness you have, the more meter will go and vice versa.

1 Like

Alright, you don’t need to use a infinity loop to do this flashlight check, it is so much inefficient and decrease game peformances so much.

You should use a “Changed” function instead, so it detect when the flashlight get enabled and disabled and then the script will run only when it is need instead of non stop running for nothing.

Here is an example:

local PlayerService = game:GetService("Players")

local Player = PlayerService.LocalPlayer
local Character = Player.CharacterAdded:Wait()

local Darkness = 0
local MaxDarkness = 10
local Status = false

------------------------------------------------------------

local function UiHandler(State)
	coroutine.resume(coroutine.create(function()
		if State == true then
			repeat task.wait(0.1)
				Darkness += 1
				script.Parent:TweenPosition(UDim2.new(-1, Darkness, 0,0), "Out", "Quad", 0)
			until
			Status == false or Darkness == MaxDarkness
		elseif State == false then
			repeat task.wait(0.1)
				Darkness -= 1
				script.Parent:TweenPosition(UDim2.new(-1, Darkness, 0,0), "Out", "Quad", 0)
			until
			Status == true or Darkness == 0
		end
	end))
end

local function FlashlightHandler(SelectedCharacter)	
	local Head = Character and Character:WaitForChild("Head", 30)
	local Att = Head and Head:WaitForChild("PlayerLightAttachment", 30)
	local Flashlight = Att and Att:WaitForChild(Player.Name.. "'s Flashlight", 30)

	if Flashlight then
		Flashlight:GetPropertyChangedSignal("Enabled"):Connect(function()
			Status = Flashlight.Enabled
			UiHandler(Flashlight.Enabled)
		end)
	end
end

------------------------------------------------------------

FlashlightHandler(Character)
Player.CharacterAdded:Connect(function(NewCharacter)
	FlashlightHandler(NewCharacter)
end)
1 Like