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

How am I able to change the rotation of a Part on Touch without it floating?

Whenever I attempt to change the Rotation of am Part using the 'onTouched' function, it floats above my Players head. My script I use:

There is a script inside of the Part I have been testing.

Part = script.Parent
Part.Anchored = true Part.Rotation = Vector3.new(0,0,0) Part.Position = Vector3.new(-6.5,1.1,7)

function onTouched() Part.Rotation = Vector3.new(0,89.98,0) wait(2) Part.Rotation = Vector3.new(0,0,0) end Part.Touched:connect(onTouched)

1 comment

0

Remember to accept the answer if it helped you and both you and Perci will get points

1 answer

You really should put your code in a code block and tab it correctly, as it makes it much easier to read. Since this is so short, however, I can still be of help.

CFrame, in Roblox, ignores other bricks. Therefore you can use CFrame.Angles to rotate a brick while ignoring other bricks. You should also add a debounce to help prevent glitching. A debounce is just a variable that prevents the code from running a second time until it's already finished running the first time. This is very useful for Touched events, because the way they work they may fire a bunch of times if the person remains standing on the brick.

Also the first few lines outside the function are kind of unnecessary, as you can just make those edits in studio.

local part = script.Parent --It's normally considered best to make variables lower case, your choice though. You should make them local, however, since it increases readability and efficiency. 
local debounce = false

function onTouched()
	if not debounce then
		debounce = true --Prevents the code from running
		part.CFrame = CFrame.Angles(0,math.rad( 89.98), 0)
		wait(2)
		part.CFrame = CFrame.Angles(0, 0, 0)
		debounce = false --Lets the code run again.
	end
end

2 comments

1

If you only change the Angle of the part using CFrame, the brick will be positioned at 0,0,0. Also, CFrame.Angles is measured in radians, so 89.98 should be math.rad(89.98). The correct way of writing the CFrame would be: CFrame.new(Position) * CFrame.Angles(math.rad(RotX),math.rad(RotY),math.rad(RotZ)).

0

Thank you. (I'm a beginner noob at scripting, as you can see.)

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