Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why do I need the wait() function in my script?

Asked by
TMGOR 20
7 years ago
Edited 7 years ago
CanTeleport = script.Parent.Value.Value
CanTeleport = true

script.Parent.Touched:connect(function(hit)
    if CanTeleport == true then
        CanTeleport = false 
        if hit.Parent:FindFirstChild("Humanoid") ~= nil then
            hit.Parent.Head.CFrame = CFrame.new(114, -34.1, -368)
            CanTeleport = true
        wait(1)
    end
        end
end)

The script above was not working, after the second time of using it. It worked the first time fine, but then after the second time it said "Head is not a Part of Accessory" or something like that.

After adding wait(0.2) to line 6 it worked, but what I just don't understand is why. Why do I need to allow the computer to wait for 0.2 seconds in order for it to function properly? As you probably know I'm new to scripting so please try to be.. simplistic with answering.

0
You shouldn't have to wait. Can you reproduce the exact error? I tested this script in studio, without a wait, but couldn't reproduce the error. duckwit 1404 — 7y
0
Works fine for me as well. The error seems to convey that there's a Humanoid in your Hat's (Accessory), which is the only way I could reproduce that error. Azarth 3141 — 7y
0
Yeah, it looks like it's just coincidental and you're hitting the wrong part of the brick.. if i'm reading it right (i'm tired.) Jric0 7 — 7y

Answer this question