Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago
Edited 6 years ago

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?

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 TheGreatSailor 20 — 6y
0
If you are still confused I can help you out further. TheGreatSailor 20 — 6y
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 TheGreatSailor 20 — 6y

1 answer

Log in to vote
0
Answered by
Nikkulaos 229 Moderation Voter
6 years ago
Edited 6 years ago

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)
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? Konethorix 197 — 6y
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 Konethorix 197 — 6y
0
You could use a while wait() loop and check the players magnitude that way, then make the billboard gui visible. Nikkulaos 229 — 6y
Ad

Answer this question