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

What does attempt to index nil with 'Position' mean?

Asked by 1 year ago
script.Parent.MouseButton1Click:Connect(function(hit)
    hit.Position = Vector3.new(-18, 0.5, -9)
end)

here's my code, it's very simple - I want the person who clicks the button to be teleported to the coordinates above. Am I misusing the event function?

1 answer

Log in to vote
0
Answered by 1 year ago

MouseButton1Click does not have a parameter called hit that returns the player's character. You need to access the player's character by using game.Players.LocalPlayer.CharacterAdded: Wait(). Here's an example:

local char = game.Players.LocalPlayer.CharacterAdded:Wait()


script.Parent.MouseButton1Click:Connect(function()
    char.Position = Vector3.new(-18,0.5,-9)
end)

This is only for use in a local script, which will not change anything for the server or the other players.

This is what you use for a normal (server) script:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        script.Parent.MouseButton1Click:Connect(function()
            character.HumanoidRootPart.CFrame=CFrame.new(Vector3.new(-18,0.5,-9)
        end)
    end)
end)

This is what you are looking for. Am I correct?

0
Thanks so much! I'm doing the LocalScript version but it doesn't seem to be working. I'm putting a print statement under the function for thee MouseButton1Click event and it's not being run. I wonder why? sebastian1102 11 — 1y
0
Can I see the code specifically to identify what your problem is? Finty_james 269 — 1y
Ad

Answer this question