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

How do i get the position of LocalPlayer?

I'm quite new to scripting in Lua and i was wondering how to get the position of LocalPlayer. My current script is:

local model = game.Workspace.Carlel
local position = game.Players.LocalPlayer.Character.Torso.Position

function onClicked()
local a = model:clone()
a.Parent = game.Workspace
wait()
a:MakeJoints()
wait()
a:MoveTo(position + Vector3.new(0,0,10))
end
script.Parent.MouseButton1Click:connect(onClicked)

I'm trying to make a car spawn near the player when the button is clicked. I've looked everywhere.

1 comment

0

Please accept my answer, it'll mark the question as done.

2 answers

✓ Accepted answer

I would use "HumanoidRootPart" instead of "Torso", as "HumanoidRootPart" is the same for both rig types.

Also, make sure that this is running in a LocalScript, as LocalPlayer can only be called from a LocalScript, and not from a Global Script.

local model = game.Workspace.Carlel
local position = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position

function onClicked()
	local a = model:clone()
	a.Parent = game.Workspace
	wait()
	a:MakeJoints()
	wait()
	a:MoveTo(position + Vector3.new(0,0,10))
end
script.Parent.MouseButton1Click:connect(onClicked)

If this is not what you are looking for, or this does not help, feel free to comment and tell me so I can alter the answer to what best suits your needs.

Cheers!

2 comments

0

Thank you! This worked. I would upvote if i could, but i do not have enough reputation.

0

If it worked, you should set this as 'answered'.

The issue here is that you save the Position of the character at the beginning of the script, everytime you call the onClicked function it tries to once again load the initial position, you need to obtain the position of the character again everytime you click the button.

Also don't forget to use the example provided by oCrxptic using the HumanoidRootPart, it's still an overall way better thing to use as opposed to using the plain Torso, this way you can have compatability between R6 and R15,

1 comment

0

You were right! The car always spawned at the spawner brick.

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