Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Player not teleporting when joined?

Asked by
MattVSNNL 620 Moderation Voter
3 years ago

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)
0
Nvm I needed to add a wait MattVSNNL 620 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

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)
0
It's ok bro, But thanks for the help MattVSNNL 620 — 3y
Ad
Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
3 years ago

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)

Answer this question