I don't know why its not working

I don’t know why its not working, I want the player to teleport to a part every time they spawn but it does not work

Script:

local part = script.Parent
while true do
	player.CharacterAdded:Connect(function(char)
		char:FindFirstChild("HumanoidRootPart").CFrame = part.CFrame
	end)
	wait(0.1)
end

output has no errors

You don’t need to use a while loop here. The CharacterAdded runs every time the player respawns.

I have removed it from the while loop but it still does not work. Any other suggestions?

Is that all of your code or just the part of it?

I have added a print() inside of the player.CharacterAdded event and it prints but I don’t get teleported

Cant you just do

player.CharacterAdded:Connect(function(char) 
 char:FindFirstChild("HumanoidRootPart").CFrame = part.CFrame
end)

Its an event, everytime the character gets added it fires.

Try using WaitForChild instead of FindFirstChild.

That’s what I am using and its not working

Try WaitForChild as @SansariGames said.

I just tried that but it does not work.

Hmm that is odd. Are you using localscript?

No, I am using a server script

image
(arranged like this)

local player = game:GetService("Players")
local part = script.Parent

player.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char) 
		char:FindFirstChild("HumanoidRootPart").CFrame = part.CFrame
	end)
end)

so this didn’t work

but this did

local player = game:GetService("Players")
local part = script.Parent

player.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char) 
		task.wait(.1)
		char:FindFirstChild("HumanoidRootPart").CFrame = part.CFrame
	end)
end)

for me, adding a task.wait(.1) fixed it.
U can leave the task.wait() blank if u want, still works.

here is the place if u want it:
part cframe.rbxl (28.0 KB)

1 Like

Hi, So I only noticed now that I did not full past my script so here is my actual full scripts (I am trying to make a jail and I sorta new a scripting)

local part = script.Parent
local player

local command = {}
for word in string.gmatch(part.Name, "%S+") do -- finding player from name
	table.insert(command, word)
end
local name = command[1]
player = game.Players:FindFirstChild(name)
player.CharacterAdded:Connect(function(char)
	char:FindFirstChild("HumanoidRootPart").CFrame = part.CFrame
end)

try to check if player is nil or not

I put the wait before the char:FindFirstChild() line and it worked

1 Like