On Roblox’s onboarding tutorials, there is this line of code which fires a touched event
for _, healthPickup in ipairs(healthPickups) do
print(healthPickup)
healthPickup:SetAttribute("Enabled", true)
print(healthPickup)
healthPickup.Touched:Connect(function(otherPart)
print(healthPickup)
onTouchHealthPickup(otherPart, healthPickup)
end)
end
However, I expect the event to only fire when the last healthPickup that has been iterated through is touched, not the healthPickup before that, Touched seems to magically know which healthPickup has been touched, and it seems like the value of healthPickup also magically changes in the function.
Full Code:
local MAX_HEALTH = 100
local ENABLED_TRANSPARENCY = 0.4
local DISABLED_TRANSPARENCY = 0.9
local COOLDOWN = 20
local healthPickupsFolder = workspace:WaitForChild("HealthPickups")
local healthPickups = healthPickupsFolder:GetChildren()
local function onTouchHealthPickup(otherPart, healthPickup)
if healthPickup:GetAttribute("Enabled") then
local character = otherPart.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health = MAX_HEALTH
healthPickup.Transparency = DISABLED_TRANSPARENCY
healthPickup:SetAttribute("Enabled", false)
wait(COOLDOWN)
healthPickup.Transparency = ENABLED_TRANSPARENCY
healthPickup:SetAttribute("Enabled", true)
end
end
end
for _, healthPickup in ipairs(healthPickups) do
print(healthPickup)
healthPickup:SetAttribute("Enabled", true)
print(healthPickup)
healthPickup.Touched:Connect(function(otherPart)
print(healthPickup)
onTouchHealthPickup(otherPart, healthPickup)
end)
end
Developer Hub Page:
I am not too sure what your issue is. Could you rephrase the issue you have and what you would like to happen.
The for loop here loops through a folder of healthPickups, the healthPickups are numbered 1-7, when the for loop ends, healthPickup should and does have a value of 7, however, if you put a print statement inside the function that prints out healthPickup’s value, healthPickup’s value becomes whatever healthPickup I touched.
For example, I touch healthPickup 1 and it prints out healthPickup 1, it prints out 7 when the print() is not inside the function.
I would expect the Touched event to only fire when I touch healthPickup 7 (not that I would WANT that to happen, but that’s whats supposed to happen as far as I understand)
Nothing inside of the code checks to see if the healthPickup is 7 from my knowledge. I am a little confused on how your code works due to there being no event for the “for_, healthPickup in ipairs” to run?
What part of your code did you think would make it so it would only run if the healthPickup was only 7?
Also why are you running this function called “otherPart” when there is no function in this script called that??

I think you have misunderstood, the function isn’t called otherPart, it’s the parameter that is getting passed onto the function call nested inside of it.
I don’t understand why the Touched function would fire if the healthPickup I touched isn’t healthPickup 7, because since healthPickup has a value of 7 on its final iteration, healthPickup.Touched should basically mean when healthPickup7 is touched it would fire.
What is stopping the event from fireing then? Surely there should be an if statment or smthing.
It shouldn’t fire if I touch any HealthPickup other than HealthPickup 7, but it does fire and it seems to magically know which HealthPickup I touched (because the healthPickup value magically changes when the Touched event has fired, seen by the print statement inside the function)
I do not believe this is the case, as each event is created for each of the health pickups hence it would fire for each individual unique health pick up object.
It doesn’t create the event for one part then overwrite it with another part. This event connection will still exist within the script itself until the script has been destroyed or the part has been destroyed.
Do you have a code sample in which how you believe the touch event behaves as you expected it?
I would expect it to behave something more like this:
for healthPickup= 0, 7,1 do
healthPickup.Touched:Connect(function(touched))
end
Since healthPickup should be 7 by the time the game loads, Touched should only fire when healthPickup 7 is touched, because it is basically 7.Touched.
(sorry if I’m repeating myself, don’t really understand why the loop doesn’t overwrite the value of healthPickup)