Hello !
Recently, i made a script where you have to touch a part, and then the part value changes to another basepart.
Here is a simple remake of the code:
local part = workspace.Part_1
part.Touched:Connect(function()
part = workspace.Part_2
end
The issue is that, when the basepart changes, the .Touched function doesn’t fire for the new part.
I tried to modify my code a little bit but nothing changed.
That’s because you assign the Touched event to the actual part, not the variable. So when you change the part that the variable holds, you also have to assign the Touched event to that part too. Just add another part.Touched:Connect and it will work.
Or just don’t make them a variable and say workspace.Part_1.Touched and then workspace.Part_2.Touched
yes, but the value changes because it depends of the number of ‘checkpoints’ there are in a folder. so, if there are like 15 parts, i can’t do 15 times “.touched”
You can also make a toTouch variable, and check if it is in the name of the part, and only do the magic if it is
local group = --the group or folder with the parts
local toTouch = 1
for i,p in pairs(group:GetChildren()) do
p.Touched:Connect(function()
if p.Name == "Part_"..toTouch then
--magic
end
end)
end
But then you have to make sure that all of the parts are named correctly
checkpointsnum = 0
nextcheckpoint = 1
obbyvalidated = false
local Obby = hit:FindFirstChild("Destination").Value.Parent
local ct = {}
for i, v in pairs(Obby:GetChildren()) do
if string.find(string.lower(v.Name), "checkpoint") then
checkpointsnum += 1
print("NAME: "..v.Name)
local checkpointnumber = string.split(v.Name, "_")[2]
print("NUMBER: "..checkpointnumber)
ct[tonumber(checkpointnumber)] = v
v.BrickColor = BrickColor.random()
if tostring(checkpointnumber) ~= "1" then
print(v.Name)
v.CanTouch = false
else
v.CanTouch = true
end
end
end
print("THERE ARE "..checkpointsnum.." CHECKPOINTS IN TOTAL")
print(nextcheckpoint)
print(ct[nextcheckpoint])
--ct[nextcheckpoint].Touched:Connect(function()
for i, v in pairs(ct) do
v.Touched:Connect(function()
if nextcheckpoint + 1 <= checkpointsnum then
ct[nextcheckpoint].CanTouch = false
nextcheckpoint += 1
print("C" .. nextcheckpoint)
ct[nextcheckpoint].CanTouch = true
end
end)
end