For a game I'm making, i need to make a script which lets a player click an object while holding a certain key on their keyboard to affect the object. How do i do this?
✓ ANSWER ACCEPTED
How to make a script to test if a player is holding a key and clicking?
1 answer
✓ Accepted answer
local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local UIS = game:GetService("UserInputService")
local key = Enum.KeyCode.E
local IsDown = Instance.new("BoolValue")
IsDown.Parent = plr
IsDown.Name = "IsDown"
UIS.InputBegan:Connect(function(key,gameProc)
if not gameProc then
if key.KeyCode == key then
IsDown.Value = true
end
end
end)
UIS.InputEnded:Connect(function(key,gameProc)
if not gameProc then
if key.KeyCode == key then
IsDown.Value = false
end
end
end)
IsDown.Changed:Connect(function(property)
if IsDown.Value == true then
mouse.Button1Down:Connect(function()
--do code
end)
end
end)
3 comments
0
no no. NEVER DO ends with )'s inside of ends WITHOUT. ITS UGLY AND UNPROFESSIONAL. Instead of an event, do Event:Wait() then the code
0
how is it ugly
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
https://scriptinghelpers.org/blog/posting-good-questions-and-answers-on-scripting-helpers