I'm making a plot system and I wanted to code that when the player joins they get teleported to their plot, But for some reason it doesn't work, I've tried everything like searching it up but it doesn't work, May anyone help me?
Btw no errors
Code:
local Players = game:GetService("Players") local plotTemplate = game:GetService("ReplicatedStorage"):FindFirstChild("Plot") Players.PlayerAdded:Connect(function(player) local clonePlot = plotTemplate:Clone() clonePlot.Name = player.Name.."'s Plot" clonePlot.Parent = workspace:FindFirstChild("Plots") player.CharacterAdded:Connect(function(char) if char then char:FindFirstChild("HumanoidRootPart").CFrame = clonePlot:FindFirstChild("SpawnPoint").CFrame end end) end)
Adding a wait is always a bad solution regardless of context, it means that the code wasn't well planned. In this case, you should just wait for the player character, and once this one exists call the function. A better solution would be:
local Players = game:GetService("Players") local plotTemplate = game:GetService("ReplicatedStorage"):FindFirstChild("Plot") local function TeleportToPlot(plot, character) character.PrimaryPart.CFrame = plot:FindFirstChild("SpawnPoint").CFrame end Players.PlayerAdded:Connect(function(player) local clonePlot = plotTemplate:Clone() clonePlot.Name = player.Name.."'s Plot" clonePlot.Parent = workspace:FindFirstChild("Plots") --Attach the event player.CharacterAdded:Connect(function(char) --This is a helper method TeleportToPlot(clonePlot, char) end) --Call it on the first player add local character = player.Character or player.CharacterAdded:Wait() TeleportToPlot(clonePlot, character) end)
Solved!
If players are having same issue then here's a code to help
Players already get teleported before they spawn
so lets add a wait
local Players = game:GetService("Players") local plotTemplate = game:GetService("ReplicatedStorage"):FindFirstChild("Plot") Players.PlayerAdded:Connect(function(player) local clonePlot = plotTemplate:Clone() clonePlot.Name = player.Name.."'s Plot" clonePlot.Parent = workspace:FindFirstChild("Plots") player.CharacterAdded:Connect(function(char) if char then char:FindFirstChild("HumanoidRootPart").CFrame = clonePlot:FindFirstChild("SpawnPoint").CFrame end end) end)