Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Player not teleporting when joined?

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)

1 comment

0

Nvm I needed to add a wait

2 answers

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)

1 comment

0

It's ok bro, But thanks for the help

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.