Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I detect if a player is touching a part and the player presses a key?

I am trying to detect if a player is touching a part and if the player presses a key something happens. I get no errors and nothing happens when I am touching the part and I press E.

My attempt:

local part = script.Parent

local function onPartTouched(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
			if inputObject.KeyCode == Enum.KeyCode.E then
				print("E was pressed")
			end 
		end)
	end
end

part.Touched:Connect(onPartTouched)

3 comments

0

is it in a localscript?

0

@NeonPandaEyes331 No, it is in a server script.

0

@NeonPandaEyes 331 I also tried it in a LocalScript it still does not work.

2 answers

OnTouched only works server-side, while checking whether the player presses a key is client-side. How I would do it is I would put a script inside of StarterPlayerScripts that makes a value for whether the player is touching the part or not and then check whether the value is true or not after pressing the E key inside of a seperate local script, also placed in StarterPlayerScripts. I would then change this value's value to true when the part is touched and then back to false when the part is stepped off of

--localscript in StarterPlayerScripts

local Mouse = game.Players.LocalPlayer:GetMouse()
--you can also use UserInputService if you desire

Mouse.KeyDown:Connect(function(key)
	if key == "e" then
		if Value.Value == true then
			--run script
		end
	end
end
--normal script inside of StarterPlayerScripts

local Value = Instance.new("BoolValue")
Value.Name = "TouchingPart"
Value.Value = false
--normal script inside of the part you touch

script.Parent.Touched:Connect(function(hit)
	if hit.Parent and hit.Parent:FindFirstChild("TouchingPart") then
		local Value = hit.Parent:FindFirstChild("TouchingPart")
		Value.Value = true
	end
end

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent and hit.Parent:FindFirstChild("TouchingPart") then
		local Value = hit.Parent:FindFirstChild("TouchingPart")
		Value.Value = false
	end
end

4 comments

0

tell me if there are any errors

0

Errors:

0

Errors: 16:16:33.931 - Workspace.Part.Script:10: Expected ')' (to close '(' at line 3), got 'script' /// 16:16:35.345 - Players.BloxianParrot.PlayerScripts.LocalScript:12: Expected ')' (to close '(' at line 6), got

2

touched works fine on the client, idk where you heard that touched only works on the server

local part = script.Parent
local UIS = game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)

part.Touched:Connect(function(hit)
	if hit and hit.Parent then
		if inputObject.KeyCode == Enum.KeyCode.E then
			local hum = hit.Parent:WaitForChild("Humanoid")
			if hum then
				print("e")
			end
		end
	end
end)

idk what i did so tell me if it works lol

1 comment

0

Doesn't work in a LocalScript or a ServerScript

This discussion is preserved as it was. New answers, comments, and votes are closed.