.Touched and Key F being press not working?

I’m trying to get an event to fire if the player is touching an object and pressing the key F.
My issue, is that it doesn’t work. I originally tried with the event firing from the keypress, but I changed in hopes that it would work. Can you please help me?

local localplayer = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local touching = false

uis.InputBegan:Connect(function(input)
	if (uis:GetFocusedTextBox()) then
		return; -- make sure player's not chatting!
	end
	if input.KeyCode == Enum.KeyCode.F then
		if touching == true then
			print("fire event")
		end
	end	
end)

game.Players.PlayerAdded:Connect(function(Player)
	script.Parent.Touched:Connect(function(hit)
		local humanoidHit = hit.Parent:FindFirstChild("Humanoid")
		if humanoidHit ~= nil then
			if hit.Parent.Name == Player.Name ~= nil then
				touching = true
			end
		end
	end)
end)

script.Parent.TouchEnded:Connect(function()
	touching = false
end)
1 Like

I’m very confused on why you’re doing key input like that iv never seen it like that before lol.

uis.InputBegan:Connect(function(key, gpe)
     if gpe then return end
     if key.KeyCode == Enum.KeyCode.F then
     end
end)

This is a more normal way of writing it.

Side note I just now saw you’re doing PlayerAdded which would indicate this is running from a server script. Key input runs client side > since its player specific.

Well, the touched event doesn’t work in localscripts afaik; so there’s one issue, and also if this is in a serverscript; there is no way to get uis from there.

This is in a script in the object that is being touched

Is this a local or server script? If it’s a local script, then it won’t run if parented to a part in the workspace. If it’s a server script, then you can’t get player input.

I recommend using Remote Events which allow you to access the server from the client. For example.

Local Script:

--Services
local RS = game:GetService('ReplicatedStorage')
local UIS = game:GetService('UserInputService')

--Remote Events
local sendTouched = RS:WaitForChild('Touched') --In this case it would be the remote event.

UIS.InputBegan:Connect(function(input)
      if input.KeyCode == Enum.KeyCode.F then
          sendTouched:FireServer() --Fires to the server script so when the key is pressed it can be used from the server
      end
end)

Server Script:

--Services
local RS = game:GetService('ReplicatedStorage')

--Remote Events
local sendTouched = RS:WaitForChild('Touched') --In this case it would be the remote event.

game.Players.PlayerAdded:Connect(function(Player)
	script.Parent.Touched:Connect(function(hit)
		local humanoidHit = hit.Parent:FindFirstChild("Humanoid")
		if humanoidHit ~= nil then
			if hit.Parent.Name == Player.Name ~= nil then
				touching = true
			end
		end
	end)
end)

sendTouched.OnServerEvent:Connect(function() --The remote event from the client so when the key is pressed this event will run.
          script.Parent.TouchEnded:Connect(function()
          touching = false
      end)
end)

A lot more better than what you are doing, of course this is an example and may not work which is my mistake. I recommend you look at the document for Remote Events.

.Touched can be used on both the client and server.

I tried this and it works, but the problem is that after the f key is pressed once, it keeps firing the remote event when duplicated parts are touched. I tried with some debounce kind of things, but it didn’t work. I honestly have no idea what to do next, do you have any ideas that could help?