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

Loops, If's, KeyDown and BackGroundTransparency help?

As before. What I'm trying to do is get a GUI to become transparent when the player presses "Left CTRL" and visible when they press "Left ALT". But with a catch, trying to make the loop, which I struggle with...

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
 
Mouse.KeyDown:connect(function(Key)
    if Key == string.char(50) then
		if script.Parent.BackgroundTransparency == 1 then
			for i = 1,0.6,-0.1 do
   				script.Parent.BackgroundTransparency = i
				wait(0.1)
			end
		end
	elseif Key == string.char(52) then
		if script.Parent.BackgroundTransparency == 0.6 then
			for i = 0.6,1,0.1 do
   				script.Parent.BackgroundTransparency = i
				wait(0.1)
			end
		end
	end
end)

--string.char(52) = Left Alt
--string.char(50) = Left Ctrl

1 comment

0

I strongly suggest using UserInputService, as KeyDown is a deprecated method. Other than that I don't see anything that is incorrect, what isn't working exactly?

1 answer

✓ Accepted answer

First things first:

KeyDown is deprecated, so it would be better if you used UserInputService. It's as simple as this (recommended to be a LocalScript):

local service = game:GetService("UserInputService") -- gets the service
service.InputBegan:connect(function(keyPressed,gameProccessedEvent) -- the latter is whether they are, for example, typing, which would mean it would be false
	if keyPressed.KeyCode == Enum.KeyCode.LeftShift then -- checks to see if it is a certain key
		print("pressed")
	end
end)

So to make it without the loop (which is better, I think):

local service = game:GetService("UserInputService") -- gets the service
local gui = script.Parent

service.InputBegan:connect(function(keyPressed,gameProccessedEvent) -- the latter is whether they are, for example, typing, which would mean it would be false
	if keyPressed.KeyCode == Enum.KeyCode.LeftShift then -- checks to see if it is a certain key
		gui.BackgroundTransparency = 1
	elseif keyPressed.KeyCode == Enum.KeyCode.LeftAlt then
		gui.BackgroundTransparency = 0
	end
end)

With a loop instead:

local service = game:GetService("UserInputService") -- gets the service
local gui = script.Parent
local enabled = false

service.InputBegan:connect(function(keyPressed,gameProccessedEvent) -- the latter is whether they are, for example, typing, which would mean it would be false
	if keyPressed.KeyCode == Enum.KeyCode.LeftShift then -- checks to see if it is a certain key
		enabled = true
	elseif keyPressed.KeyCode == Enum.KeyCode.LeftAlt then
		enabled = false
	end
end)

while wait() do
	if enabled == true then
		gui.BackgroundTransparency = 1
	else
		gui.BackgroundTransparency = 0
	end
end

Hope I helped :)

Be sure to tell me in the comments if you need more help.

~TDP

1 comment

1

The loop he meant was the one that interpolates the GUI's transparency

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