Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
kraigg -2
9 years ago

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)

0
Remember to accept the answer if it helped you and both you and Perci will get points ZeptixBlade 215 — 9y

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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
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)). TurboFusion 1821 — 9y
0
Thank you. (I'm a beginner noob at scripting, as you can see.) kraigg -2 — 9y
Ad

Answer this question