Why is this script not working?

So I’m making a script inside a part where if a player touches this part they will get teleported to another part called “SpawnTP” in-game, I am using the code below but for some reason, it isn’t working? (P.S. please explain thoroughly as I’m not a good scripter)

script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild(“HumanoidRootPart”) then
hit.Parent.HumanoidRootPart.Position = script.Parent.Parent.SpawnTP.Position
end
end)

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild('HumanoidRootPart') then
		hit.Parent.HumanoidRootPart.CFrame = script.Parent.Parent.SpawnTP.CFrame
	end
end)

If there aren’t any errors, this should work.

1 Like

Try this, If that doesn’t work Please tell me the error.

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
--- I recommend make it CFrame
hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = script.Parent.Parent.SpawnTP.CFrame
end
end)
1 Like

Are there any errors in the output?

You can open the output while playing in the view tab. I don’t see a problem with the script, I suppose there’s something wrong with the instances.

1 Like

For some reason, it didn’t work but now it does, it didn’t show any output errors too that is why I was confused.

HumanoidRootPartCFrame

Missing a period here.

Would be better if you both used pivots instead of directly assigning the character’s root’s ‘CFrame’ property.

2 Likes