Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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?

2 comments

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?

0

Can I see the code specifically to identify what your problem is?

This discussion is preserved as it was. New answers, comments, and votes are closed.