Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago
Edited 6 years ago

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

Script

1local key = "e"
2 
3mouse.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

01local plr = game.Players.LocalPlayer
02local key = "e"
03local mouse = plr:GetMouse()
04 
05mouse.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 
03local UserInputService = game:GetService("UserInputService")
04local plr = game:GetService("Players").LocalPlayer
05local mouse = plr:GetMouse()
06 
07UserInputService.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.Part666.Transparency = 0.5
15        end
16    end
17end)

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

Please help!

0
you are missing 3 ends SulaymanArafat 230 — 6y
0
by the way KeyDown is deprecated you could use inputbegan from userinputservice ill make an answer rn SulaymanArafat 230 — 6y

3 answers

Log in to vote
2
Answered by 6 years ago

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

01-- Place in StarterPlayerScripts
02 
03local UserInputService = game:GetService("UserInputService")
04local plr = game:GetService("Players").LocalPlayer
05local mouse = plr:GetMouse()
06 
07UserInputService.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
16end)
0
loca script? RainbowBeastYT 85 — 6y
0
Of course! Local scripts are for user input. Never use a server script to do this. User#19524 175 — 6y
0
you're such a smarty pants incap i love it chomboghai 2044 — 6y
0
I made a new script, but did not work is this correct? RainbowBeastYT 85 — 6y
View all comments (5 more)
0
Make sure it's a local script under either StarterGui, StarterPack, or StarterPlayerScripts. User#19524 175 — 6y
0
It is RainbowBeastYT 85 — 6y
0
still does nto work RainbowBeastYT 85 — 6y
0
Make sure it's not disabled User#19524 175 — 6y
0
it is not RainbowBeastYT 85 — 6y
Ad
Log in to vote
1
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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

01local key = Enum.KeyCode.E
02local UserInputService = game:GetService("UserInputService")
03local mouse = game.Players.LocalPlayer:GetMouse()
04 
05UserInputService.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
12end)

Hope this helps :)

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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.

01local mouse = game.Players.LocalPlayer:GetMouse()
02local key = Enum.KeyCode.E
03 
04game: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
14end)

Answer this question