Function Stops midway through

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to spawn the player (the gist of what I’m doing).

  2. What is the issue? Include screenshots / videos if possible!
    When I run the script below, sometimes the function fully goes through, but other times it stops at “Function started” (at the top)

game.Players.PlayerAdded:Connect(function(plr)
print("SCRIPT IS RUNNING")

local function tpandspawn ()
	print("Function started")
	local Players = game:GetService("Players")
	print("Player service retrived")
--player needs to be defined here
local player = plr
		print(" Going to wait for character to be added")
		player.CharacterAdded:Wait()
		print("Waiting is over")
		local character = player.Character or player.CharacterAdded:Wait() -- Get the character. If it doesn't exist, wait for the event ChildAdded to be called.
		wait(1)
		print(player.Name .. " joined the game!")
		local spawns = { -- side note, the spawns probably don't need this degree of accuracy. You should be able to round off all of those decimals.
			CFrame.new(64, 16105, -3289),
			CFrame.new(114, 16105, -3289),
			CFrame.new(164, 16105, -3289),
			CFrame.new(214, 16105, -3289),
			CFrame.new(264, 16105, -3289),						
			CFrame.new(264, 16105, -3289)						}
		local function spawnPlayer()
			local hrp = character:WaitForChild("HumanoidRootPart") -- Get the HumanoidRootPart if it exists, or wait for it to exist. I can't remember if you need to recursively check for things to exist like this, but it won't hurt.
			local num = math.random(#spawns)
			print(spawns[num])
			hrp.CFrame = spawns[num] * CFrame.Angles(0, math.rad(180) , 0)

			--Check if the player has actually been teleported
			print(hrp.CFrame)
			print(character)
			print(spawns[1])
			print(spawns[2])
			print(spawns[3])
			print(spawns[4])
			print(spawns[5])
			print(spawns[6])
			table.remove(spawns, num)
			print(spawns[1])
			print(spawns[2])
			print(spawns[3])
			print(spawns[4])
			print(spawns[5])
			print(spawns[6])
			local Spawncord = hrp.CFrame
			
			print(spawns[num] * CFrame.Angles(0, math.rad(180) , 0))
			
			
			if hrp.CFrame == spawns[num] * CFrame.Angles(0, math.rad(180) , 0) then
				print("Player should be in position")
			else
				print("Error Code 565 : Player has not been spawned in position")
				--later update: Reset player for fix
			end
			--change cam
			local ReplicatedStorage = game:GetService("ReplicatedStorage")
			local Players = game:GetService("Players")

			local remoteEvent = ReplicatedStorage:WaitForChild("Camchange")

			local function Camchange ()
				-- Fire the remote event
				remoteEvent:FireClient(player, Spawncord, num)
			end
			print("function should connect")
			Camchange(Spawncord)
			print("serverside connected")

			--Anchor head and stop player from moving
			local char = player.character
			local humanoidroot = char:WaitForChild('HumanoidRootPart')
			print("devforum info")
			humanoidroot.Anchored = true
			print("Your head should be anchored")
			local humanoid = char.Humanoid
			humanoid.WalkSpeed = 0
		end
		spawnPlayer()
	end



	local function GameIntro ()			
		local ReplicatedStorage = game:GetService("ReplicatedStorage")
		local Players = game:GetService("Players")

		local remoteEvent = ReplicatedStorage:WaitForChild("Present")
		
		remoteEvent:FireClient(plr)
		
	end





local DSS = game:GetService("DataStoreService")  
local store = DSS:GetDataStore("Joins") 

	local didjoin = false 
	local firsttime = false
	local success, joined =  pcall(function()
		return store:GetAsync(plr.UserId)
	end)
	if success then
		if joined == 1 then 
			didjoin = true
			print("Welcome back friend!")
			tpandspawn()
		
		else
			store:SetAsync(plr.UserId, 1)
			firsttime = true
			print("First time yay!")
			tpandspawn()
			GameIntro()
		end
	end
	if not success then
		print("failed")
	end
end)

print("End of script")
1 Like

am I able to try and fix it on the game that its in so I have access to the datastores that are in the script?

I mean, I could give you access but what would be the reasoning? The function fires but it stops at the player added event :confused:

does it come up with failed in the output for you or does it not even get up to that point?
image

it dosen’t get up to that point

I think the reason it says failed is because the data store isn’t working for you (it works for me)

Yeah thats one reason why I wanted to have access to the game its in so I would have all the stuff like datastores and events.

I see. Well for further reference here’s the output

does it teleport the character or does that not work or get up to it?

It never get’s up to the teleporting part. It stops at

local players = game:GetService("Players")

Is it at the first local players = game:GetService(“Players”)?

the first? There’s supposed to only be one :grimacing: (Guess I messed up). But yeah, it doesn’t proceed past the first one.

I also found another thing which is that you didn’t do the math.random correct because it needs two variables like this:
image
with the lowest number you want in the first bit and the highest in the second.

try this script:

print("SCRIPT IS RUNNING")

	print("Function started")
	local Players = game:GetService("Players")
	print("Player service retrived")
	--player needs to be defined here
	local player = game.Players.LocalPlayer
	print(" Going to wait for character to be added")
	print("Waiting is over")
	local character = player.Character or player.CharacterAdded:Wait() -- Get the character. If it doesn't exist, wait for the event ChildAdded to be called.
	print(player.Name .. " joined the game!")
	local spawns = { -- side note, the spawns probably don't need this degree of accuracy. You should be able to round off all of those decimals.
		CFrame.new(64, 16105, -3289),
		CFrame.new(114, 16105, -3289),
		CFrame.new(164, 16105, -3289),
		CFrame.new(214, 16105, -3289),
		CFrame.new(264, 16105, -3289),						
		CFrame.new(264, 16105, -3289)						}
	local function spawnPlayer()
		local hrp = character:WaitForChild("HumanoidRootPart") -- Get the HumanoidRootPart if it exists, or wait for it to exist. I can't remember if you need to recursively check for things to exist like this, but it won't hurt.
		local num = math.random(1,#spawns)
		print(spawns[num])
		hrp.CFrame = spawns[num] * CFrame.Angles(0, math.rad(180) , 0)

		--Check if the player has actually been teleported
		print(hrp.CFrame)
		print(character)
		print(spawns[1])
		print(spawns[2])
		print(spawns[3])
		print(spawns[4])
		print(spawns[5])
		print(spawns[6])
		table.remove(spawns, num)
		print(spawns[1])
		print(spawns[2])
		print(spawns[3])
		print(spawns[4])
		print(spawns[5])
		print(spawns[6])
		local Spawncord = hrp.CFrame

		print(spawns[num] * CFrame.Angles(0, math.rad(180) , 0))


		if hrp.CFrame == spawns[num] * CFrame.Angles(0, math.rad(180) , 0) then
			print("Player should be in position")
		else
			print("Error Code 565 : Player has not been spawned in position")
			--later update: Reset player for fix
		end
		--change cam
		local ReplicatedStorage = game:GetService("ReplicatedStorage")
		local Players = game:GetService("Players")

		local remoteEvent = ReplicatedStorage:WaitForChild("Camchange")

		local function Camchange ()
			-- Fire the remote event
			remoteEvent:FireServer(player, Spawncord, num)
		end
		print("function should connect")
		Camchange(Spawncord)
		print("serverside connected")

		--Anchor head and stop player from moving
		local char = player.character
		local humanoidroot = char:WaitForChild('HumanoidRootPart')
		print("devforum info")
		humanoidroot.Anchored = true
		print("Your head should be anchored")
		local humanoid = char.Humanoid
		humanoid.WalkSpeed = 0
	end
	spawnPlayer()



local function GameIntro ()			
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local Players = game:GetService("Players")

	local remoteEvent = ReplicatedStorage:WaitForChild("Present")

	remoteEvent:FireServer(player)

end

I put this into a localscript in StarterCharacterScripts.

well, here’s the thing, this was in a server script so It could spawn each player in an individual box without players getting into each others’. with this model, it only does so for individual clients and it kind of defeats the purpose of the script itself

Is "Function started" being printed in this case or not? If not, see this thread.

it is being printed, and sometimes the entire thing runs, but other times it just stops at “function started”