So, I have this part of a local script:
local plr = game.Players.LocalPlayer local mouse = plr:GetMouse() local toggle = false local door = workspace.Door mouse.KeyDown:connect(function(key) if key == string.lower("r") then if toggle == false then toggle = true door.CanCollide = false else toggle = false door.CanCollide = true end end end)
So, how do I make it so that the .KeyDown event (or UserInputService.InputBegan) only available when you're near a part?
You can use magnitude to fix this script
local plr = game.Players.LocalPlayer local mouse = plr:GetMouse() local toggle = false local door = workspace.Door
mouse.KeyDown:connect(function(key) if key == string.lower("r") and (door.Position-plr.Character.HumanoidRootPart.Position).magnitude < 10 then --Magnitude detects how much units away the item is. The 10 is 10 units. if toggle == false then toggle = true door.CanCollide = false else toggle = false door.CanCollide = true end end end)