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

I use the key command/script, it did not work, am I missing anything?

I use the key command/script, it did not work, am I missing anything?

Script

local key = "e"

mouse.KeyDown:connect(function(k)
	if k:lower() == key then
		if mouse.Target then
			if mouse.Target:FindFirstChild("item") then
				script.Parent.Transparency = 0.5
			end

Fixed Script

local plr = game.Players.LocalPlayer
local key = "e"
local mouse = plr:GetMouse()

mouse.KeyDown:connect(function(k)
	if k:lower() == key then
		if mouse.Target then
			if mouse.Target:FindFirstChild("item") then
				script.Parent.Transparency = 0.5
			end
		end
	end
	end)		

Fix version

-- Place in StarterPlayerScripts

local UserInputService = game:GetService("UserInputService")
local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

UserInputService.InputBegan:Connect(function(input, gpe) -- input beginning
    if gpe then return end
     -- if the player is processing text (like chatting! and pressing key, end the function

    if input.KeyCode == Enum.KeyCode.E then -- if the key is E
        if mouse.Target and mouse.Target:FindFirstChild("item") then
           -- code
		workspace.Part666.Transparency = 0.5
        end
    end
end) 

Still did not work why? It's a local script in StarterPlayerScripts

Please help!

2 comments

0

you are missing 3 ends

0

by the way KeyDown is deprecated you could use inputbegan from userinputservice ill make an answer rn

3 answers

###You're missing two ends, and an end). Also, KeyDown is deprecated, do not use it in new work. Same goes for :connect().

-- Place in StarterPlayerScripts

local UserInputService = game:GetService("UserInputService")
local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

UserInputService.InputBegan:Connect(function(input, gpe) -- input beginning
    if gpe then return end
     -- if the player is processing text (like chatting! and pressing key, end the function

    if input.KeyCode == Enum.KeyCode.E then -- if the key is E
        if mouse.Target and mouse.Target:FindFirstChild("item") then
           -- code
        end
    end
end) 

9 comments

0

loca script?

0

Of course! Local scripts are for user input. Never use a server script to do this.

0

you're such a smarty pants incap i love it

0

I made a new script, but did not work is this correct?

0

Make sure it's a local script under either StarterGui, StarterPack, or StarterPlayerScripts.

0

It is

0

still does nto work

0

Make sure it's not disabled

0

it is not

Firstly, don't use mouse.KeyDown, it's deprecated. Instead, use UserInputService. Also, use Enum to specify the character, don't use a string.

local key = Enum.KeyCode.E
local UserInputService = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()

UserInputService.InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.KeyCode == key then
		if mouse.Target and mouse.Target:FindFirstChild("item") then
			script.Parent.Transparency = 0.5
		end
	end
end)

Hope this helps :)

Your code has 3 missing ends, so your 2 if statements and your function aren't closed. Also, using KeyDown is deprecated, UserInputService is the best choice for input.

With the InputBegan event we could make a function, get the input and check if it's the keycode E and if its then run the code.

local mouse = game.Players.LocalPlayer:GetMouse()
local key = Enum.KeyCode.E

game:GetService("UserInputService").InputBegan:Connect(function(input,gpe)
    if gpe then return end -- This will return if being fired while you're focused in a coregui, for example
    
    if input.KeyCode == key
        if mouse.Target then
            if mouse.Target:FindFirstChild("item") then
                script.Parent.Transparency = 0.5
            end
        end
    end
end)

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