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

Why is my transparency script not working when i press a key?

So, i am trying to make a script where when you press e some objects hove 0 transparency, for some reason this script wont work. Why?

Here is the script, it is local and it is in Starter pack.

repeat wait() until game.Players.LocalPlayer
local player = script.Parent.Parent
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key == "e" then
    game.Workspace.eno.Transperency = 0
	game.Workspace.owt.Transperency = 0
	game.Workspace.eerht.Transperency = 0
	game.Workspace.ruof.Transperency = 0
	end
end)

1 comment

0

Is this is a local script?

1 answer

✓ Accepted answer

Use UserInputService, also you were spelling 'Transparency' incorrectly.

UserInputService, somewhat like ContextActionService, will provide you a way to detect all sorts of input on the client. It includes events such as:

  • InputBegan: when an input begins, such as pressing a key down.
  • InputEnded: when an input ends, such as letting go of said key.
  • InputChanged: when an input is changed, useful for things like mouse movement.

All of these events can be connected to a function with an "input" argument, which you'll use to check what kind of input is being received, as well as the optional boolean "gameProcessedEvent." gameProcessedEvent will be true if the input is being handled elsewhere in the game, such as typing in the chat.

Values of input can be checked using the Enum library, such as Enum.KeyCode

Here is a solution that will work for you:

local player = game.Players.LocalPlayer
local userInput = game:GetService("UserInputService")

userInput.InputBegan:Connect(function(input, processed)
	if not processed and input.KeyCode == Enum.KeyCode.E then
		game.Workspace.eno.Transparency = 0
		game.Workspace.owt.Transparency = 0
		game.Workspace.eerht.Transparency = 0
		game.Workspace.ruof.Transparency = 0
	end
end)

Keep in mind, if you want the Transparency changes to replicate to the server and other clients, you'll need to use RemoteEvents/RemoteFunctions and change the transparency in a server script.

More can be learned about UserInputService/Enums here: https://developer.roblox.com/api-reference/class/UserInputService https://developer.roblox.com/api-reference/enum/KeyCode https://developer.roblox.com/api-reference/enum/InputType

2 comments

0

No real explanation = bad answer.

0

Sorry for so many edits, just getting used to the site. Also Phlegethon I've elaborated quite a bit.

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