I’ve a script that detects and changes a part’s name on PlayerAdded and I want to teleport the player to the part that has the same name as their but it doesn’t seem to set it.
No errors in the output, script is in ServerScriptService.
local Players = game:GetService("Players")
local playerHangerRegionsFolder = workspace["Player Hangar Regions"]
local DEFAULT_PART_NAME = "Part"
Players.PlayerAdded:Connect(function(player)
for _, hangar in pairs(playerHangerRegionsFolder:GetChildren()) do
if hangar.Name == DEFAULT_PART_NAME then
hangar.Name = player.Name
break
end
end
player.CharacterAdded:Connect(function(character)
print("Fired") -- prints..
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
humanoidRootPart.CFrame = playerHangerRegionsFolder[player.Name].CFrame + Vector3.new(0, 5, 0)
end)
end)
No its not the same thing, CFrames will do it in object space while the vector will do it in world space (CFrame | Documentation - Roblox Creator Hub), anyways this is what you need to do:
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local playerHangerRegionsFolder = workspace["Player Hangar Regions"]
local DEFAULT_PART_NAME = "Part"
Players.PlayerAdded:Connect(function(player)
for _, hangar in pairs(playerHangerRegionsFolder:GetChildren()) do
if hangar.Name == DEFAULT_PART_NAME then
hangar.Name = player.Name
break
end
end
local Character = player.Character or player.CharacterAdded:Wait()
RunService.Stepped:Wait()
Character:SetPrimaryPartCFrame(playerHangerRegionsFolder[player.Name].CFrame + Vector3.new(0, 5, 0))
end)
First, you know you can do :FindFirstChild(DEFAULT_PART_NAME) instead of looping and finding one yourself (and I know that under the hood it loops anyways, but it look a lot cleaner).
Second, if it’s not setting position the way you expect, maybe you aren’t finding a hangar that has the default name (Part)? It doesn’t make sense otherwise… Either the default name isn’t “Part” or your hangar folder is empty based on what I’m seeing. It could be something else but I’m not sure, I can’t think of anything else it could be. Try adding a print inside the loop to see what it’s doing.