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

Making a key pressing system which is only available when near a part?

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?

3 comments

0

You could put a click detector in the door so when the mouse is hovering it sets the toggle to true, and when the hover leaves it sets the toggle to false. Depending on if your using filtering enabled which I recommend you will have to use remote events to trigger the local script

0

If you are still confused I can help you out further.

0

Alternatively you could have an invisible part in the area you want the player to be able to open the door and detect if player is touching it and then turn toggle on

1 answer

✓ Accepted answer

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)

3 comments

0

Good answer, but I have another question: i want to customize it so that when the player gets near a billboard gui will pop up and then the player can press the key. Will i need to use loops?

0

And when the player goes out of range, the gui will be gone and the event will be disconnected. Its like the Jailbreak key pressing system

0

You could use a while wait() loop and check the players magnitude that way, then make the billboard gui visible.

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