Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

how do i move the characters "root part"?

Asked by 3 days ago

i have a script here:

game.Players.PlayerAdded:Connect(function(player)
    local user = player.Character
    player.Chatted:Connect(function(msg)
        if msg == "code:123!x556" then
            user.HumanoidRootPart.Position = Vector3.new(0,0,0)
        end
    end)
end)

and i have this error Workspace.Script:5: attempt to index nil with 'HumanoidRootPart' i dont even know what this means lol help please

2 answers

Log in to vote
1
Answered by
skvyy 153
3 days ago

The issue here is that you are calling the HumanoidRootPart before it is loaded into the game.

A quick fix to this would be adding a

player.CharacterAdded:Wait()

to your code.

The finished version is below!

local secretMessage = 'test'

game.Players.PlayerAdded:Connect(function(plr)
    local char = plr.Character or plr.CharacterAdded:Wait()
    plr.Chatted:Connect(function(messageSent)
        if messageSent == secretMessage then
            char.HumanoidRootPart.Position = Vector3.new(0,0,0)
        end
    end)
end)
Ad
Log in to vote
0
Answered by
SirGamezy 152
3 days ago

The error could be because you're calling the character immediately. In most cases, the character doesn't load as soon as the player joins, so you can try replacing local user = player.Character with local user = player.CharacterAdded:Wait() so the character is retrieved when it is loaded.

Answer this question