Not Teleporting With Nothing in Output

I’m awful at building so I’m using some maps that I didn’t make, and, of course, they don’t have SpawnLocations. To combat this without having to go through and replace all of them with SpawnLocations, I wanted to design a script that teleports a player when they spawn.

local maps = game.ReplicatedStorage.Maps:GetChildren()
local map = maps[math.random(1, #maps)]
map.Parent = workspace

local Players = game:GetService("Players")
 
local function onCharacterAdded(character)
	if map then
		if map:FindFirstChild("Spawns") then
			local items = map.Spawns:GetChildren()
			local randomItem = items[math.random(1, #items)]
			character.HumanoidRootPart.Position = Vector3.new(randomItem.Position.X, randomItem.Position.Y + 3,randomItem.Position.Z)
		elseif map:FindFirstChild("Spawn") then
			local items = map.Spawn:GetChildren()
			local randomItem = items[math.random(1, #items)]
			character.HumanoidRootPart.Position = Vector3.new(randomItem.Position.X, randomItem.Position.Y + 3,randomItem.Position.Z)
		end
	end
end
 
local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

However, nothing happens and there are no errors in Output on either the Client or Server.
The if statement is because some of them have a model named Spawns and others Spawn and changing them would be hell.

You don’t need to do this:

character.HumanoidRootPart.Position = Vector3.new(randomItem.Position.X, randomItem.Position.Y + 3,randomItem.Position.Z)

But instead this:

character.HumanoidRootPart.Position = randomItem.Position + Vector3.new(0,3,0)

Also, if you want to keep the orientation of the character, you should do CFrames.

I never actually thought about how that was possible. Simplifies a lot.

1 Like

Instead of doing that, try this.

local randomItemPos = items[math.random(1, #items)].Position
character.HumanoidRootPart.Position = randomItemPos + Vector3.new(0, 3, 0)

I had a similar problem recently. It’s possible that you renamed something in the workspace but forgot to change the name in the code. Check to make sure.

That’s the only script I wrote and the only other thing being put into workspace is my character.

try adding some prints in to figure out what line it breaks on.

1 Like

Well the issue isn’t the map. I did some testing using .Parent and .Name and everything seemed fine. It has something to do with where you actually teleport, maybe the if statements. I’ll try to experiment with those.

an elaboration on my previous reply: I think there is an error stopping the script, but it’s not actually being shown. You can use prints to figure out where the script stops.

Oh, thank you for the clarification.

I put print(“Teleported”) after the teleportation code and it showed up, so the script doesn’t stop. I’m even more confused now.

Try seeing if you can get teleportation to work outside of this context.

local Players = game:GetService("Players")
 
local function onCharacterAdded(character)
	character.HumanoidRootPart.Position = Vector3.new(100, 5, 100)
end
 
local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

.
doesn’t seem to work

local Players = game:GetService("Players")
 
local function onCharacterAdded(character)
	character.HumanoidRootPart.CFrame = Cframe.new(100, 5, 100)
end
 
local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

try this

Nothing. This is very strange. I’ll try adding a wait.

Nothing.

I am completely stumped. Maybe someone more experienced can help.

Yeah, I’ve used this same thing before and it worked fine. Maybe it has something to do with some recent updates, but I have no clue. I’ll fiddle around a bit more to see if I can get anything started.

I know this is probably useless but try and say function instead of local function. Could help.

The functions are running so it won’t do anything.

You could just not write these functions separately like the code below. I’m just trying to make your script more easier to read while we figure out the issue.

game.Players.PlayerAdded:Connect(function(p)

p.CharacterAdded:Connect(function(c)

c.HumanoidRootPart.Position = Vector3.new(100, 5, 100)

end)

end)