I’ve been trying to figure out how to make an if statement that teleports anchored parts repeatedly and
well, I cant script so I don’t need a genius to tell me that I’m doing something wrong somewhere.
the script functions but the part only moves once and then the function stops executing.
In detail, i have a child part in a parent part (both being attached by a weld) and I want the parent part to moved to the child parts position when the parent part is touched by a humanoid(me).
The parent part is anchored but the child part isn’t. so, the child part follows the parent part. then i update the child parts position.
Here’s the script
local childpart = script.Parent
local parentpart = childpart.Parent -- variables
-- Then check if it is touched
childpart.Touched:Connect(function(hit)
--If it is touched by a humanoid(me)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
--simple height limit, no need to be alarmed its just background information
if parentpart.Position.y <= 121 then
--- set position for teleportation
local childposition = childpart.Position
-- teleport
parentpart.Position = childposition
-- cus why not. just to avoid potential lag
wait(5)
--update position
childposition = childpart.Position
--confirm if the script got this far
print("It works")
end
end
end)
-- what did i do wrong???
I know the problem is probably very obvious but this only works the first time it runs.
I don’t have any idea why
Not sure, but, you should put some more print statements outside the IF and inside the connection. You also need a debounce, the two ‘it works’ that you got came from the first touch (after a 5 second delay they both showed up before you touched the child part after the part is moved).
I don’t know why you’re updating your position variable after the part is moved as that value is lost when the connection code is completed.
I don’t know why the touch isn’t triggered after you moved the parts, its a very strange thing you’re trying to do here.
Looking at your output, if you have not been using module scripts, then you may have a virus. Go to explorer search bar >> type “Vaccine” >> delete any named “Vaccine.” Just to be safe.
The y position is 120.95.
It’s just to make sure it doesn’t teleport when it is above that height.
[There are gonna be more than just one child part, I just to know that the script works.
The height limit is just kinda like ground level]
I’m so sorry to be this unspecific its been a while since I used this site
The first time, I didn’t update the position variable and i suspected that it just kept teleporting to the same position. it’s just to be sure that isn’t the problem