What do you want to achieve? I want the for loop to only execute once, or in other words, when the player gets the tool.
What is the issue? Everytime I unequip the tool it keeps executing again. I know it’s because the tool adds back to the backpack but that doesn’t help me.
What solutions have you tried so far? I tried putting it into a remote event but it didn’t work, and the problem is too specific so I can’t really find anyone having the same problem. (or there just isn’t a post about it on DevForum.)
for i,v in ipairs(game.Players:GetPlayers()) do
v.Backpack.ChildAdded:Connect(function(child)
if child.Name == "Phone" then
ToggleDialogueEvent:FireAllClients(true)
DialogueEvent:FireAllClients("Agent: Did you find anything?")
wait(2.3)
ToggleDialogueEvent:FireAllClients(false)
ObjectiveEvent:FireAllClients("Play Recording")
end
end)
end
I am a beginner when it comes to coding so if it’s a simple solution I apologize for not figuring it out. Any help is appreaciated!
Is that code in a LocalScript or a regular (server) Script? (looks like it’s server code, from “FireAllClients”)
Also, it might help to know where exactly this code is located in your Explorer.
I assumed this was the code for a tool, but after look at all of it, it looks like it’s not.
From what you’ve shown. The loop is indeed only happening once.
The reason things may be happening more than what you expect is because you are working with events. "v.Backpack.ChildAdded:Connect"
So, code that looks like it is inside the loop is “kinda not”. I mean, it is in the loop, but that doesn’t mean it can’t execute multiple times, thanks to events.
So according to that code, each time you unequip an item, the at "if child.Name == "Phone" then" will run.
For clarity, you could write it like this.
function ToolAddedToBackpack(child)
if child.Name == "Phone" then
ToggleDialogueEvent:FireAllClients(true)
DialogueEvent:FireAllClients("Agent: Did you find anything?")
wait(2.3)
ToggleDialogueEvent:FireAllClients(false)
ObjectiveEvent:FireAllClients("Play Recording")
end
end
for i,v in ipairs(game.Players:GetPlayers()) do
v.Backpack.ChildAdded:Connect(ToolAddedToBackpack)
end
I’ve moved the “inline function” out to make it clear what the loop is actually doing.
You are actually passing a reference to a function into “Connect”. So if this code is indeed in some place where it only executes once, it will only execute once.
BUT, “ToolAddedToBackpack” will still execute long after this code happens, because that is how events works. And there are many times when having code run “at a later time” is important.
The code you are showing is for loop in all players in game, each player found it will connect a function, the function is to “listen” when something is added in the backpack.
So, the loop runs once, it iterates once per player, to create a function that is listening each player backpack.
I dont understand your question. Indeed, the loop runs once, and does many iterations, once per player in game, to give them a function that is checking whats added into the backpack. If the tool added in the backpack is named “Phone”, you are Firing a remote to All players in game. Probably thats what you want. Change from FireAllClients to FireClient.
By this:
I understand you want this only happens the first time the player puts the phone into the backpack?
Then:
for i,v in ipairs(game.Players:GetPlayers()) do
local conn
conn = v.Backpack.ChildAdded:Connect(function(child)
if child.Name == "Phone" then
ToggleDialogueEvent:FireClient(v, true)
DialogueEvent:FireClient(v, "Agent: Did you find anything?")
task.wait(2.3)
ToggleDialogueEvent:FireClient(v, false)
ObjectiveEvent:FireClient(v, "Play Recording")
conn:Disconnect()
end
end)
end
It is a server script located in ServerScriptService
and unfortunately is still doesn’t work, I think the reason why it is because you just made the code into a function. The code works fine as intended, it executes when the tool is added to my backpack and it doesnt do it again EXCEPT whenever I unequip it which executes the code again.
My goal is when the tool is added to a player’s backpack it executes the code but the issue is that whenever I unequip the tool it goes back to the backpack, hence executing the code agan. Appreciate the help though.