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 make it so when you are within a radius, and you press q on your keyboard it prints?

I'm trying to make it so when you press "q" on your keyboard and you are within a radius of a brick, it will print "q" in the output. Why won't this work?

player = game.Players.LocalPlayer
Mouse = player:GetMouse()

function onTouch()
	wait()
	for i, v in pairs(game.Players:GetChildren()) do
		if (v.Character.Torso.Position - script.Parent.Position).magnitude <=4 then
			local UIS = game:GetService("UserInputService")
			UIS.InputBegan:connect(function(Key, GPE)
				if Key.KeyCode == Enum.KeyCode.Q and not GPE then
					print"q"
				end
			end)
				--print"f"
			wait()
		else
			print"not in the radius"
		end
	end
end
script.Parent.Touched:connect(onTouch)

1 answer

This is because you check for the distance between the torso and the part from outside of your InputBegan callback function; meaning, in your particular case, that the distance is being calculated on touch.

Move the if statement inside of the callback and everything should work fine.

1 comment

0

As a side note, I'd like to point out that the calls to "wait()" in your case are not necessary.

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