Script doesn't run after :LoadCharacter()

For some reason, after running :LoadCharacter, the code following it just doesn’t run at all and even when I insert a print statement after the :LoadCharacter, it doesn’t print anything, this is my code:

game.ReplicatedStorage.arrestEvent.OnServerEvent:Connect(function(plr, arrestedplrname, duration)
	if script.Parent.Parent.Name == arrestedplrname then
		cuffed = false
		grabbed = false
		local arrestedchar = script.Parent.Parent
		
		local arrestedplr = game.Players:FindFirstChild(arrestedplrname)
		arrestedplr:LoadCharacter()
	
		arrestedplr.CharacterAdded:Wait()
		local arrestedchar = workspace:FindFirstChild(arrestedplr.Name)
		arrestedchar.HumanoidRootPart.Position = workspace[math.random(1,3)].Position
		arrestedplr.PlayerGui.custodyGUI.box.Text = "You have been arrested for " .. duration .. " minutes"
		game:GetService("TweenService"):Create(arrestedplr.PlayerGui.custodyGUI.box, TweenInfo.new(2,Enum.EasingStyle.Quad,Enum.EasingDirection.Out), {Position = UDim2.new({0.152, 0},{0.251, 0})}):Play()
		wait(3)
		game:GetService("TweenService"):Create(arrestedplr.PlayerGui.custodyGUI.box, TweenInfo.new(2,Enum.EasingStyle.Quad,Enum.EasingDirection.Out), {Position = UDim2.new({0.152, 0},{-0.201, 0})}):Play()
		end
end)

I’m trying to figure out why the code after :LoadCharacter doesn’t run at all and it’s not a problem with the print statement because I’ve tested it with other code and it doesn’t run, I can’t use :LoadCharacterAsync because I’m trying to load the character into a different place and that’s when the code stops running, any help is appreciated, thanks in advance.

It’s a race condition. The player isn’t guaranteed to be loaded into the game before you start executing the code again.
You could try using a while not arrestedplr.Character do wait() end before you run the rest of your code.
You could also try using the :LoadCharacterFinished event, which you should be able to do in the console.

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