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

How do i rotate a model?[unsolved]

player = game.Players.LocalPlayer
mouse = player:GetMouse()
x=1
y=1
z=1

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
		print'r' -- just here cause
		player.Character.short:FindFirstChild("Model"):SetPrimaryPartCFrame(
CFrame.new(x,y,z),CFrame.Angles(0,math.pi,0))
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

where short is the name of a message.

sadly, the above script does not work. It's supposed to rotate the model whenever i press r. But what happens is that the model moves a little bit lower, presumably to the y position of pi. So how do i have the model be rotated?

2 comments

0

Are you Using CFrame or Position to rotate the model?

0

edited

2 answers

What Dakota said is correct however you want to remove the x, y and z variables you created and do the following instead:

local model = player.Character.short:FindFirstChild("Model")
model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame * CFrame.Angles(0,math.pi,0))

This sets the model to it's current CFrame + 180 degrees (math.pi).

If you want to rotate by 90 degrees you can do math.pi/2 or math.rad(90)

Here is how you would do it using the mouse:

local model = player.Character.short:FindFirstChild("Model")
model:SetPrimaryPartCFrame((model.PrimaryPart.CFrame - model.PrimaryPart.CFrame.p + mouse.Hit.p) * CFrame.Angles(0,math.pi,0))

You get the current CFrame of the model and minus it's position, add the mouses position and then rotate.

8 comments

0

ok, ok. Lets say i set the model CFrame with: player.Character.short:WaitForChild("Model"):SetPrimaryPartCFrame(mouse.Hit.p) .......and it does rotate, but when i update it's position with the aforementioned line of code, it will go back to it's original position, so how do i get it say at the rotation?

0

i meant that it will go back to its original rotation, the positioning is fine

0

Edited my answer for you.

0

when i run that i get this: attempt to multiply a Vector3 with an incompatible value type or nil

0

Missed off a bracket, should be fixed now!

0

this does the same thing as mine, it puts the model into the floor a little, and does not rotate

0

You do realise math.pi is 180 degree rotation and you probably might not notice it, change the rotation from math.pi to math.rad(45)

0

don't worry about it, GoldenPhysics solved the problem

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