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

Playeradded - Characteradded question?

Asked by 4 years ago

can someone explain why this code does not move the character?

local players = game:GetService("Players")

local function characterAdded(character)
    wait(3)
    character:SetPrimaryPartCFrame(CFrame.new(0, 500, 0))
end

local function playerAdded(player)
    player.CharacterAdded:Connect(characterAdded)

end

players.PlayerAdded:Connect(playerAdded)

when this code does

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(newPlayer)
    newPlayer.CharacterAdded:Connect(function(characterModel)
        wait(3)
        characterModel:SetPrimaryPartCFrame(CFrame.new(0, 500, 0))
    end)
end)
0
they both work fine for me. the first one is just written in a much more confusing way. the second snippet utilizes silent functions, which are usually preferred when connected to events, like `PlayerAdded` Gey4Jesus69 2705 — 4y
0
I think that sometimes on roblox studio the player loads before the server script, so it doesnt bind the event to the player, because he was added before that event was connected. To fix it, you can GetChildren() of players and do the same code as PlayerAdded, just before it is connected (for i=1,#players do teleportfunction(player) end game.Players.PlayerAdded:Connect(teleportfunction)). Dfzoz 489 — 4y
0
Characters might have the same problem. But I think the published version of the game doesn't have that problem, so you might not need to do this, unless for testing the game easier. Dfzoz 489 — 4y
0
Thanks for both your answers. i not shure what the right answer would be but Dfzoz you might be right, but i used the second code instead, because its seems to work better Kaptajnfar 15 — 4y

Answer this question