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

Why is the position of the player being reset when Character spawns in?

Asked by 5 years ago
Edited 5 years ago

Hello, I'm trying to change the character's position to something else right when it spawns in, but something else seems to be setting it after I change it.

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        print(character.Name .. " has spawned");
        local rig = character or character.player.CharacterAdded:Wait();
        rig.HumanoidRootPart.CFrame = CFrame.new(500,500,500);
        print (rig.HumanoidRootPart.CFrame);
    end)
end)

This prints out that the CFrame is equal to (500, 500, 500, 1, 0, 0, 0, 1, 0, 0, 0, 1), but then it changes to something like (0, 107, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1).

0
Local or Server Script? WyattagumsBackUp 5 — 5y
0
It's probably calculating the CFrame at which the Character first spawns, which happens to be over 100 studs on the Y axis while being 0 on the other two axes. I suggest putting a wait() somewhere so that when the Character spawns in the script waits then it assigns the CFrame and prints that CFrame. DeceptiveCaster 3761 — 5y
0
Thanks MCAndRobloxUnited; putting wait(0.000001) right before everything else fixed it, which seems like an oversight on Roblox's end. Could you turn your comment into an answer so I can accept it as the working solution? davidgingerich 603 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited by User#24403 4 years ago

Understand how the Character spawns in first.

If the game does not have a set spawn point (i.e. via SpawnLocation), the Character will spawn a certain amount of studs along the Y axis above the point (0, 0, 0). In your case, that happens to be 107 studs above (0, 0, 0) along the Y axis. Because you're setting the CFrame before the Character completely spawns in, the CFrame is set first and then reset to the default CFrame assuming that there is no spawn point that the developer had set before the game ran.

To fix the problem, all you need to do is add a wait():

wait(0.1)
local rig = character
character.HumanoidRootPart.CFrame = CFrame.new(500, 500, 500)
print(character.HumanoidRootPart.CFrame)
Ad
Log in to vote
0
Answered by 5 years ago

Somethin about vector3 gets in my head

I don't use cframe for spawning I use Position if you change it to the (untested) following

Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
    print(character.Name .. " has spawned");
    local rig = character or character.player.CharacterAdded:Wait();
    rig.HumanoidRootPart.Position = Vector3.new(500,500,500);
    print (rig.HumanoidRootPart.CFrame);
end)
end)

it should work...

0
It should not work... DeceptiveCaster 3761 — 5y
0
I already tried changing the position instead of the CFrame, it did the same thing. davidgingerich 603 — 5y

Answer this question