Script wait function not working

wait(2)

script.Parent.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild(“Humanoid”) then

hit.Parent.HumanoidRootPart.CFrame = CFrame.new(46, 60.25, -110.919)

end

end)

Could you elaborate on what you’re trying to make?

1 Like

the wait function doesn’t work.

example it should wait 2 seconds before it teleports a player.

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then wait(2)
hit.Parent.HumanoidRootPart.CFrame = CFrame.new(46, 60.25, -110.919)
end
end)

This script will wait 2 seconds before teleporting player that touched the part:

script.Parent.Touched:Connect(function(hit)
  if hit.Parent:FindFirstChildWhichIsA(“Humanoid”) then
        task.wait(2)

        hit.Parent.HumanoidRootPart.CFrame = CFrame.new(46, 60.25, -110.919)
     end
end)
1 Like

This is still fatly I do not know why.

Well right now you have it waiting, then waiting again for a player to touch before it “teleports them”.

I suggest doing something like this:

task.delay(2, function()
	
	script.Parent.Touched:Connect(function(hitPart)
		
		if (hitPart.Parent:FindFirstChildWhichIsA('Humanoid')) then
			
			hitPart.Parent:MoveTo(Vector2.new(46, 60.25, -110.919))
			
		end
		
	end)
	
end)
2 Likes

If the player is touching the part before the code activates it won’t work, as they didn’t touch it, they are touching it already, they will have to get off the part and step back on it for this to work properly.

1 Like

Bro, I make such stupid mistakes. I have a question how do you know what to put in the code?

I just have a lot of experience with studio, if you are learning youtube helps alot as well as:

Wait do you know why, in 6 years Roblox has never asked me to commit something and now it is asking me? If I don’t it won’t let me publish the scripts. I think it said commit. Today is the first time it ever showed this.

Open studio, go to the home tab, open game settings, go to “other” and turn off collaborative editing.

1 Like

it is not working for me. I do not know why.

Humanoid doesn’t get picked up and then.

Sorry that was my bad. I accidentally used incorrect quotations in my post try this instead:

script.Parent.Touched:Connect(function(hit)
  if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
        task.wait(2)

        hit.Parent.HumanoidRootPart.CFrame = CFrame.new(46, 60.25, -110.919)
     end
end)

This doesn’t check if the player exists the part. I’m confused on that psrt

I am confused. Can you please elaborate more on what you are trying to achieve?

Edit: The coding example I have displayed above checks if the player that touched the part has a Humanoid which is what every player has and if it doesn’t, further code will not execute.

I think you haven’t understood what your script does here:

  1. The wait function works, but it is not readable by human means(it finished before game loaded fully).
  2. A touched event is connected, which checks for its parent’s humanoid. If it exists, teleport to a fixed space.

The wait() method is also superseded by task.wait(), and the former nowadays is somewhat inaccurate.

3 Likes

Have you tried using a debounce?