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.
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.
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.
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)
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)
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.
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.