If a player approaches one of the two doors (the two doors are in a folder), it will only open up that one door.
Server Script:
nearEvent.OnServerEvent:Connect(function(plr)
for i, door in pairs(doors:GetChildren()) do
local dist = (plr.Character:FindFirstChild("Torso").Position - door[i]:FindFirstChildOfClass("Model").MainPart.Position).magnitude
if dist < setting.radius and deb2 == false then
deb2 = true
print("Gui Fade In")
guiFadeIn()
setting.deb = true
deb = false
end
if dist > setting.radius and deb == false then
deb = true
print("Gui Fade Out")
guiFade()
setting.deb = false
deb2 = false
end
end
end)
Local Script:
for i, door in pairs(doors:GetChildren()) do
local dist = (player.Character:FindFirstChild("Torso").Position - door[i]:FindFirstChildOfClass("Model").MainPart.Position).magnitude
if dist < setting.radius or dist > setting.radius then
nearEvent:FireServer()
end
end
local setting = {
radius = 12, -- Distance the player must be near the door for them to interact with it (studs)
camSpeed = 0.7, -- Speed of the camera zooming into the doors
doorSpeed = 1.25, -- Speed of the doors opening
doorOpenStyle = Enum.EasingStyle.Sine, -- Refer to this link if you want to change opening/easing styles (https://developer.roblox.com/en-us/api-reference/enum/EasingStyle)
doorSound = "157167203", -- Sound the doors make when opening
}
--[[ Instructions for pretty much everything --
* Keep everything in a folder inside of the 'doors' folder
* Make sure to weld everything you want to move/animate, to the 'hingePart'. If you don't already,
here's a link to a plugin that automatically welds everything to one part. (https://www.roblox.com/library/148570182/Weld-Plugin)
* Feel free to move the 'begDest', 'camPart', 'endDest', 'guiPart', 'moveToPart' to wherever you want, as long as it's inside of the folder.
]]
return setting
It’s for a commission so I provided some instructions for the person (saying this incase someone thinks its a free model)
Minor unrelated things regarding the code but if you’re going to index with . right after anyways don’t use FindFirstChild, since with or without you will error.
Prefer player.Character.Torso.Position to player.Character:FindFirstChild("Torso").Position, the latter is more typing and operations for worse code.
Also for tables you get from GetChildren, always use ipairs over pairs. GetChildren returns an array which is to be iterated with the array iterator ipairs.
Since it returns an array, it also solves your problem as you can directly access it for any element you want using my_children[n] style.
If you name the doors like “door1” “door2”. You can easily pass through a string with the remote Event. Like this
local:
NearEvent:FireServer(door.Name)
server:
nearEvent.OnServerEvent:Connect(function(plr, doorName)
doors:FindFirstChild(doorName) -- Through this you get the door that was 'touched' and you can now do something with it.
end)
A way you can do this is to create a table of all available doors to interact with and then insert them into an array. You can then sort this table based on the distance the character is to the door and then return the first indice of the array, which will be the door closest to the player.
If u wanna search for specific child just use Instance:FindFirstChild() or Instance:WaitForChild()
in this way you don’t need to get all children when u need only one
Edit. you can use it even when you already need all children
Look at the topic again. The specific use case is to get one item out of a table of them. The table happens to be one of doors and OP only wants the nearest one to open. In a streamlined structure, this won’t be a viable solution because it’ll only get the first child matching a specific name. It doesn’t allow you to filter results and pick the one that best matches.
ok maybe I was wrong with findfirstchild I mean it could be but it would be longer so I made this script (ye it uses for loop)
`game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local area = Instance.new("Part", workspace) -- add new part that is area around player
area.Name = player.Name .. "'sArea"
area.Size = Vector3.new(10,0.1,10) -- Size of area in studs
area.Transparency = 1 -- transparency
area.CanCollide = false -- cancollide to false
area.Anchored = true -- make it anchored
while wait(0.0001) do -- infinite loop with delay
area.CFrame = char.HumanoidRootPart.CFrame -- make area be where you
for _,v in pairs(area:GetTouchingParts()) do
-- do something
end
end
end)
I know this topic is rather old, however, I think the fix for it would be to add break inside the if statements scope.
Why I think this would fix the problem you have which is that all the doors are opening, is because it’s not gonna continue the loop if it has found the door.
Now you already have the specific door child you want, as it is defined in the for key, value loop.
The fixed code would be
nearEvent.OnServerEvent:Connect(function(plr)
for i, door in pairs(doors:GetChildren()) do
local dist = (plr.Character:FindFirstChild("Torso").Position - door[i]:FindFirstChildOfClass("Model").MainPart.Position).magnitude
if dist < setting.radius and deb2 == false then
deb2 = true
print("Gui Fade In")
guiFadeIn()
setting.deb = true
deb = false
break
end
if dist > setting.radius and deb == false then
deb = true
print("Gui Fade Out")
guiFade()
setting.deb = false
deb2 = false
break
end
end
end)