I made a simple script that teleports you to the spawnlocation when you touch a part, but for some reason the script doesn't work and there are no errors.
local Teleport = game.Workspace.SpawnLocation script.Parent.Touched:Connect(function(plr) plr.Character.HumanoidRootPart.CFrame = CFrame.new(Teleport.CFrame.X, Teleport.CFrame.Y + 5, Teleport.CFrame.Z) end)
READ EDIT AT BOTTOM
Hello! I am here to help. Usually, for teleport scripts, I use .Position, since whenever I use CFrame, it always returns an error about CFrame not existing, and I'm not sure why. Anyways, that is my recommendation. In addition, you would use a new Vector3 instead of making a new CFrame. Let me show you an example of what I use for teleport scripts.
local teleport = game.Workspace.TeleportDestination local part = script.Parent part.Touched:Connect(function(hit) if hit.Parent:FindFirstChildWhichIsA("Humanoid") then hit.Parent.Humanoid.Position = Vector3.new(teleport.Position.X, teleport.Position.Y + 5, teleport.Position.Z) print('Success') --This will only print if the code runs correctly; if it does not print, then there was an error. end end)
Before using the script, make sure the spawn point is anchored. (it should be by default) Hope this helps!
EDIT: So after looking at your question again and the script, I realized you did:
Note that the .Touched event does not pass through the player arguement. Instead, it passes through the bodypart/part that touched the part. In this case, it would pass through probably the player's leg(s) as the bodypart. This would mean the parent would be the player's character. Therefore, do:
script.Parent.Touched:Connect(function(part) local character = part.Parent local humanoid = character:FindFirstChildWhichIsA("Humanoid") end)
Again, hope this helps!
You should read the description of the Touched event here. The Touched event comes with a parameter known as the part that otherPart. So, you're getting confused and think that the parameter is the player while it's not.
local Players = game:GetService("Players") local Teleport = workspace.SpawnLocation script.Parent.Touched:Connect(function(hitPart) local character = hitPart.Parent if character:FindFirstChild("Humanoid") then character.HumanoidRootPart.CFrame = CFrame.new(Teleport.CFrame.X, Teleport.CFrame.Y + 5, Teleport.CFrame.Z) end end)
First, I got the
Players service. Why did I use the
:GetService method while I could've done
game.Players? Because if you changed the name,
:GetService method would still work.
Line 2 is a variable for the destination.
Line 4 is connecting a Touched event to a function.
Line 5 is the character while sometimes it may not be.
Line 7 is to confirm if its character then we look at its children for a humanoid then if it return an Instance meaning it's true, otherwise, returns nil meaning false.
Line 8 is teleporting the character.