I know how to do keydown, but how would I do it so it would only trigger if you were touching a part. I was thinking finding the child of the character if persay I had script.Parent.Touched:connect(Function(hit) and if hit was nil to the player it wouldn't work? I don't know, honestly I'm lost. Please help!
Keydown if touching a block, how would I go about this? Sorry for posting so much
1 answer
What you could do here, is use the TouchEnded event. This event fires when you step off the brick.
What to do:
Create two variables: One being the part you want them to touch and the other a bool which will determine if the keydown will work or not.
local canKeyDown=false;
local part=game.Workspace:WaitForChild("Part")
Afterwards. lets create our .Touched function
part.Touched:connect(function(hit)
if(hit.Parent:FindFirstChild("Humanoid").Health~=nil and game.Players:GetPlayerFromCharacter(hit.Parent)) then
canKeyDown=true;
end
end)
In the code above, we basically say, if the object that touched it is a player, then proceed. We then set our bool variable to true, so the keydown will work.
After that, we need to add our KeyDown event..
part.Touched:connect(function(hit)
if(hit.Parent:FindFirstChild("Humanoid").Health~=nil and game.Players:GetPlayerFromCharacter(hit.Parent)) then
canKeyDown=true;
local player=game.Players:GetPlayerFromCharacter(hit.Parent) --Using Getplayerfromcharacter to get the player
local mouse=player:GetMouse()--Gets the players mouse
mouse.KeyDown:connect(function(key)--Running the keydown event
if(canKeyDown and key=="r") then--If keydown==true and you press "R" then print.
print("can keydown")
end
end)
end
end)
Now that we have our Touched event done, we need to add our TouchEnded event...
Again, we use Getplayerfromcharacter to check if the player is indeed a player, we then set canKeyDown to false so we can't execute the keydown function.
part.TouchEnded:connect(function(hit)
if(hit.Parent:FindFirstChild("Humanoid")and game.Players:GetPlayerFromCharacter(hit.Parent)) then
canKeyDown=false;
end
end)
The full script should look like this:
local canKeyDown=false;
local part=game.Workspace:WaitForChild("Part")
part.Touched:connect(function(hit)
if(hit.Parent:FindFirstChild("Humanoid").Health~=nil and game.Players:GetPlayerFromCharacter(hit.Parent)) then
canKeyDown=true;
local player=game.Players:GetPlayerFromCharacter(hit.Parent)
local mouse=player:GetMouse()
mouse.KeyDown:connect(function(key)
if(canKeyDown and key=="r") then
print("can keydown")
end
end)
end
end)
part.TouchEnded:connect(function(hit)
if(hit.Parent:FindFirstChild("Humanoid")and game.Players:GetPlayerFromCharacter(hit.Parent)) then
canKeyDown=false;
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
If you need a better explanation, simply inform me so I can edit my answer. I did my answer rather fast
Thanks for the answer, just a little confused. Reply to me on roblox, if you have any extra time to help me ;c