What's wrong with my script exactly?

I’m making a custom random spawning system for practicing purposes, but it doesn’t work.

Code in a ServerScript, stored in ServerScriptService.

local collectionService = game:GetService("CollectionService")
local spawnLocationsTag = collectionService:GetTagged("SpawnLocations")
-- The spawnLocationsTag includes 9 Parts, representing spawn locations. All of them are named 'SpawnLocation1', 'SpawnLocation2', 'SpawnLocation3', 'SpawnLocation4', and so on.

print("Test") -- Prints successfully
game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	local spawnLocationValue = math.random(1, 9)

	print("Test") -- Prints successfully
	print(spawnLocationValue) -- Prints successfully

	for i, spawnLocation in (spawnLocationsTag) do
		print("Test") -- Prints successfully

		if spawnLocation.Name == "SpawnLocation" .. spawnLocationValue then
			humanoidRootPart.CFrame = spawnLocation.CFrame + Vector3.new(0, 10, 0)
			print("Test") -- Prints successfully
		end
	end
end)

No errors in the output, only the prints.

2 Likes

All of your prints are named ‘Test’, have you ensured that the correct ones are being printed and it’s not just the loop-print being repeated several times?

1 Like

Yes

1 Like

And the behaviour is just that everything is printed but the character isn’t teleported?

Yes, everything prints but the player doesn’t teleport. They just teleport to the center of the world.

And if I add Roblox’s default spawn location, the player automatically teleports to it.

Out of curiosity, what happens if you add a wait(2) before you start the for-loop?

2 Likes

Nothing happens, everything acts the same but instead there’s a two-second delay.

1 Like

So you spawn else-where, wait 2 seconds, then teleport to the centre of the map?

1 Like

Oh wait nvm it actually worked!

Thank you so much!

2 Likes

No worries, I imagine it was interfering with the default spawning behaviour of Roblox.

1 Like

It would be good to have the client do the teleporting. Have the server send a remote event to the new position. It might break if the client is super laggy.

2 Likes

Just did that.

Thanks for the suggestion! Appreciated.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.