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

help with character positioning?

in a game i am coding when someone opens a door a little animation will play of them touching the keypad for the animation to like nice i need to teleport the character to the right distance away then face the character towards the keypad i can teleport the character but i am stumped on trying to face the character on a angle when right now if someone clicked the keypad on a angle the animation would play but it would be completely off i tried multiple tutorials but they don't work any help?

2 answers

✓ Accepted answer

A quick and dirty way of moving a player's character (without changing their rotation) is using the "Model:MoveTo()" function (and a Vector3 in the curly braces to set a position).

But since you are seeking help to turn I would recommend the more complicated method of using the "Model:SetPrimaryPartCFrame()" function in order to both move and rotate the player.

Assuming the player clicks on a keypad in the "physical world" by clicking on parts via a ClickDetector you can get the player's model via this method:

game.Workspace.Part.ClickDetector.MouseClick:Connect(function(Player)
	local CharacterModel = Player.Character --Gets the player's character model
	
	CharacterModel:SetPrimaryPartCFrame(CFrame.new(x, y, z) * CFrame.Angles(math.rad(degreesX), math.rad(degreesY), math.rad(degreesZ)))
	--[[
	You can also do the following which is what the function does as well:
	CharacterModel.PrimaryPart.CFrame = CFrame.new(yadda yadda code stuff...) * CFrame.angles(yadda yadda more code stuff...)
	]]--
end) --The ClickDetector always returns the player who clicked on it

Details about the "Model:SetPrimaryPartCFrame()" here: https://developer.roblox.com/en-us/api-reference/function/Model/SetPrimaryPartCFrame

If you want to get the model's CFrame for other purposes then lookie here: https://developer.roblox.com/en-us/api-reference/function/Model/GetPrimaryPartCFrame

If you have any questions or issues contact me. ;)

7 comments

0

the end) is highlighted red which means i haven't been able to try the the code yet but i cant tell why it is red any help

0

I forgot to add an extra curley brace at the end of the ":SetPrimaryPartCFrame()" function. I'll just edit that so you can go ahead and test it. :)

0

hello again i have tested it ounce more but every time i try to put in my own x, y, z coordinates it wont work the script itself says their are no problems and i tried fixing it to no success can you help me

0

Try placing a part around it. For me it looked like it wasn't moving at all until I realized that it was moving very slowly. Lastly, makue sure that you have a proper PrimaryPart set.

0

i feel so dumb i am consistently doing testing and research but somehow i still cant wrap my head around how to get the data for the degrees part and just generally get the code to work. am i just being really dumb? here is the data that i have put in (CFrame.new(9, 0, 80) * CFrame.Angles(math.rad(9), math.rad(0), math.rad(80)))

0

I reccomend changing which data is what in the degrees part if you can't turn the player around. Maybe the first one could be "math.rad(80)" or it could even be the second one. The thing for players is that if you rotate them in in upward or sideways (but not like turning around) motion then the caracter will automatically put itself back up which is what might be happenenig.

0

thank you for helping me with this the code works now after i figured out that i'm just really dumb and had a different name for the part that was in the script as the one n the workplace thank you again

https://developer.roblox.com/en-us/api-reference/datatype/CFrame

Change the CFrame of the user's HumanoidRootPart to a position p that looks at position a like so character.HumanoidRootPart.CFrame = CFrame.new(p, a)

You would have to find p. I would suggest using the keypads location and adding an offset of a few studs. a would just the the position of the keypad

1 comment

0

i tried this but the character part come up with problems (sorry if im being dumb im new to lua

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