I’m trying to make a door so that if a player interacts with the ProximityPrompt in it, the prompt will check for tools in a table, and if a tool matches a name in the table it’ll open, and if not it’ll decline the player. The short script that’s down below isn’t printing any errors or doing anything to the door
local cards = {
"L1",
"L2",
"L3",
"L4",
"L5",
"Omni",
}
prompt1.Triggered:Connect(function(Player)
for _, tool in pairs(Player.Backpack:GetChildren()) do
if table.find(cards, tool) then
openDoors()
prompt1.Enabled = false
prompt2.Enabled = false
wait(3.8)
closeDoors()
wait(.25)
prompt1.Enabled = true
prompt2.Enabled = true
else
declined()
end
end
end)
maybe its because i tried to get the player from a server script (although im pretty sure you can do this with the .Triggered:Connect(function() thingy)
And also, adding an if tool then and wrapping if table.find() into it should fix it not working with no tools at all.
Sorta like this:
prompt1.Triggered:Connect(function(Player)
for _, tool in pairs(Player.Backpack:GetChildren()) do
if tool then
if table.find(cards, tool) then
openDoors()
prompt1.Enabled = false
prompt2.Enabled = false
task.wait(3.8)
closeDoors()
task.wait(.25)
prompt1.Enabled = true
prompt2.Enabled = true
else
declined()
end
end
end
end)
I also changed the wait()'s to task.wait() because it’s better to use.
wait() works on 1/30th of a second, so if a user has less frames than another, it’ll take run less frequently and take longer to finish. Someone with 15 fps would take way more time than someone with 120 fps.
However, task.wait() is the exact same thing, it just queues threads differently. It makes it way more efficient than wait()
It’s good practice to use task.wait() instead. But there are times where you CAN’T use task.wait(), but that gets complicated and I don’t have much time to explain that now.
By the way, for the for loop, is there a way I can do for _, tool in pairs(Player.Backpack:GetChildren() or Player.Character:GetChildren()) do so that if the player equips the tool it’ll still open? (sorry for extending this thread in advance lol)
First of all, sorry for responding a bit late. I’m pretty busy right now.
And second of all, yes, there is!
Try this. This should work:
local function doorOpen()
openDoors()
prompt1.Enabled = false
prompt2.Enabled = false
task.wait(3.8)
closeDoors()
task.wait(0.25)
prompt1.Enabled = true
prompt2.Enabled = true
end
prompt1.Triggered:Connect(function(Player)
for _, tool in pairs(Player.Backpack:GetChildren()) do
if table.find(cards, tool.Name) then
doorOpen()
else
declined()
end
end
if Player.Character then
for _, charTool in pairs(Player.Character:GetChildren()) do
if charTool:IsA("Tool") and table.find(cards, charTool.Name) then
doorOpen()
else
declined()
end
end
end
end)
What it’s doing is it’s checking everything in the player’s character model the same way with the backpack. But it checks if the object is a tool, then it checks if that tool’s name is in the cards table. After that, if everything succeeds, it opens the doors!
Just paste that code into your script (replacing the old prompt1.Triggered event of course).
It does work, but when I interact with the prompt, it apparently plays both the openDoors() and decline() at the same time due to the fact that if the card is in the player’s backpack, then it won’t be in the player’s character, and vice versa, which ends up doing this:
edit: i noticed that you made another local function from my existing function, was that just for the task.wait?
Ah! My bad. Didn’t think about that!
I’ll have to work on that a bit later though. I’m very busy atm as I said.
Also no, that wasn’t for the task.wait. I did that to make it look a little cleaner in the code. I don’t like copy and pasting that much code into pretty much the same exact thing.
Also, one other quick question. Can you send me the place file? It makes it way easier to test stuff. You can send it via private message if you don’t want it to be public, but you also don’t have to at all! It would help significantly, though.