Hello everyone,
I’m writing a script where an NPC will be spawned. The Player can interact with it and give it cake, but when I give cake to the NPC, the connection will not be disconnected.
I give cake to one NPC, and it works.
I give cake to another NPC, the first NPC will register it too.
I give cake to the third NPC, and the first two NPCs will register it too.
Here’s the code:
coroutine.wrap(function()
repeat wait() until OrderNumber == OrderQueue[1]
ProximityPrompt.Enabled = true
local connection : RBXScriptConnection
connection = ProximityPrompt.Triggered:Connect(function(Cashier)
moduleScript.DisplayBubble(NPC, "Hello, I would like the cake on "..cakeNumber..".")
ProximityPrompt.Enabled = false
task.spawn(function()
wait(15)
ProximityPrompt.Enabled = true
end)
for _, placement : BasePart in ipairs(Placements:GetChildren()) do
local placementProximityPrompt: ProximityPrompt = placement:FindFirstChildWhichIsA("ProximityPrompt")
placementProximityPrompt.Triggered:Connect(function(Player)
if Player ~= Cashier then return end
if placementProximityPrompt.Parent.Name == tostring(cakeNumber) then
connection:Disconnect()
print("right cake")
else
print("not this one")
end
end)
end
end)
end)()
task.spawn(function()
repeat wait() until OrderNumber == OrderQueue[1]
ProximityPrompt.Enabled = true
local connection : RBXScriptConnection
connection = ProximityPrompt.Triggered:Connect(function(Cashier)
moduleScript.DisplayBubble(NPC, "Hello, I would like the cake on "..cakeNumber..".")
ProximityPrompt.Enabled = false
task.spawn(function()
wait(15)
ProximityPrompt.Enabled = true
end)
for _, placement : BasePart in ipairs(Placements:GetChildren()) do
local placementProximityPrompt: ProximityPrompt = placement:FindFirstChildWhichIsA("ProximityPrompt")
placementProximityPrompt.Triggered:Connect(function(Player)
if Player ~= Cashier then return end
if placementProximityPrompt.Parent.Name == tostring(cakeNumber) then
connection:Disconnect()
print("right cake")
else
print("not this one")
end
end)
end
end)
end)
I’ve tried that as well. Now I have the idea of listing every placement connection into the table and disconnecting them all after I give the right cake. Wait a sec.
The problem was when I disconnected ProximityPrompt.Triggered:Connect(function(Cashier) … end) so NPC won’t talk anymore, but it will continue in registering placementProximityPrompt.Triggered:Connect(function(Player) … end) and that was the problem.
To solve it I made a table with connections and after I gave the right cake, every single connection got disconnected.