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

How to enable a script in a specific area?

Asked by 4 years ago

I want to make an area when a player steps in, a keybind script will be enabled. How do I make this area to know when the player step in, like for example I'm making a keybind shop, the player stand near the stall and press a key, the shop GUI will show up? I've already have the keybind code, I just need the area to work. Thanks!

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
    if key == "F" then
        game.StarterGui.ScreenGui.ScrollingFrame.Visible = true
    end
end)
0
Keydown is not reccomended. They say you should resort to using ContextActionService or UserInputService. KDarren12 705 — 4y
0
":connect" is deprecated. It's recommended to use ":Connect" but both work the same. SilverCreeper58 23 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

you could also use region3 if you want them to be in a very specific point, ex. you dont want them to be able to access the shop from behind. they have to be within the region3 directly in front of your shop.

you can use FindPartsInRegion3 and check if part.Name == "HumanoidRootPart", and if true then you can press F to open the shop. I actually did this today and it worked very well :)

local inRegion = false

while wait(1) do

for _, Part in pairs(game.Workspace:FindPartsInRegion3(Region3.new(), nil, math.huge)) do 

if Part.Name == "HumanoidRootPart" do

local player = Part.Parent

if player then

local humanoid = player.Humanoid

if humanoid then

inRegion = true

if inRegion then

--your code

wait(1)

inRegion = false

end

end

end

end

end

sorry if code looks bad did it on my phone

--> LINE 5 <-- the Region3.new() takes 2 arguments, 2 opposite vectors that create a rectangular prism.

if you have a part you can get the opposing 2 vectors like this:

Region3.new(brick.Position - (brick.Size/2), brick.Position + (brick.Size/2)

I hope I helped you, I tried my best :)

Ad
Log in to vote
0
Answered by 4 years ago

You could use DistanceFromCharacter or istouching function. I would prefer distance. Essentially the script would be checking if a player is in the vicinity of a part, and if so, the keybind would be enabled.

local part_area = workspace:FindFirstChild("keybind_part")
local size_area = 30 -- 30 or less studs from the center of part_area
game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key) -- change connect to Connect for new code
    if key == "F" then
        if game.Players.LocalPlayer:DistanceFromCharacter(part_area.Position) <= size_area then -- Player in range
            game.StarterGui.ScreenGui.ScrollingFrame.Visible = true
        end
    end
end)

Just change the first two lines to your desire. part_area should be a part, and this part will mark the position of the area's center. size_area is how far away a player can be from the center of the part_area in studs to be able to use the keybind.

Also, please consider using UserInputService as KeyDown is deprecated.

0
thanks! MinuhaYT 19 — 4y

Answer this question