I use the key command/script, it did not work, am I missing anything?
Script
1 | local key = "e" |
2 |
3 | mouse.KeyDown:connect( function (k) |
4 | if k:lower() = = key then |
5 | if mouse.Target then |
6 | if mouse.Target:FindFirstChild( "item" ) then |
7 | script.Parent.Transparency = 0.5 |
8 | end |
Fixed Script
01 | local plr = game.Players.LocalPlayer |
02 | local key = "e" |
03 | local mouse = plr:GetMouse() |
04 |
05 | mouse.KeyDown:connect( function (k) |
06 | if k:lower() = = key then |
07 | if mouse.Target then |
08 | if mouse.Target:FindFirstChild( "item" ) then |
09 | script.Parent.Transparency = 0.5 |
10 | end |
11 | end |
12 | end |
13 | end ) |
Fix version
01 | -- Place in StarterPlayerScripts |
02 |
03 | local UserInputService = game:GetService( "UserInputService" ) |
04 | local plr = game:GetService( "Players" ).LocalPlayer |
05 | local mouse = plr:GetMouse() |
06 |
07 | UserInputService.InputBegan:Connect( function (input, gpe) -- input beginning |
08 | if gpe then return end |
09 | -- if the player is processing text (like chatting! and pressing key, end the function |
10 |
11 | if input.KeyCode = = Enum.KeyCode.E then -- if the key is E |
12 | if mouse.Target and mouse.Target:FindFirstChild( "item" ) then |
13 | -- code |
14 | workspace.Part 666. Transparency = 0.5 |
15 | end |
16 | end |
17 | end ) |
Still did not work why? It's a local script in StarterPlayerScripts
Please help!
end
s, and an end)
. Also, KeyDown
is deprecated, do not use it in new work. Same goes for :connect()
.01 | -- Place in StarterPlayerScripts |
02 |
03 | local UserInputService = game:GetService( "UserInputService" ) |
04 | local plr = game:GetService( "Players" ).LocalPlayer |
05 | local mouse = plr:GetMouse() |
06 |
07 | UserInputService.InputBegan:Connect( function (input, gpe) -- input beginning |
08 | if gpe then return end |
09 | -- if the player is processing text (like chatting! and pressing key, end the function |
10 |
11 | if input.KeyCode = = Enum.KeyCode.E then -- if the key is E |
12 | if mouse.Target and mouse.Target:FindFirstChild( "item" ) then |
13 | -- code |
14 | end |
15 | end |
16 | end ) |
Firstly, don't use mouse.KeyDown
, it's deprecated. Instead, use UserInputService
. Also, use Enum
to specify the character, don't use a string.
01 | local key = Enum.KeyCode.E |
02 | local UserInputService = game:GetService( "UserInputService" ) |
03 | local mouse = game.Players.LocalPlayer:GetMouse() |
04 |
05 | UserInputService.InputBegan:Connect( function (input, gp) |
06 | if gp then return end |
07 | if input.KeyCode = = key then |
08 | if mouse.Target and mouse.Target:FindFirstChild( "item" ) then |
09 | script.Parent.Transparency = 0.5 |
10 | end |
11 | end |
12 | 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.
01 | local mouse = game.Players.LocalPlayer:GetMouse() |
02 | local key = Enum.KeyCode.E |
03 |
04 | game:GetService( "UserInputService" ).InputBegan:Connect( function (input,gpe) |
05 | if gpe then return end -- This will return if being fired while you're focused in a coregui, for example |
06 |
07 | if input.KeyCode = = key |
08 | if mouse.Target then |
09 | if mouse.Target:FindFirstChild( "item" ) then |
10 | script.Parent.Transparency = 0.5 |
11 | end |
12 | end |
13 | end |
14 | end ) |