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!
end
s, 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)
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)