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 5 years ago
Edited 5 years ago

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!

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

3 answers

Log in to vote
2
Answered by 5 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().

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

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 :)

Log in to vote
1
Answered by 5 years ago
Edited 5 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.

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)

Answer this question