I'm trying to make a block rotate and have any player standing on it move with the rotation of the block but no matter how I go about coding it, the block just rotates without also moving the players standing on it.
Here is the code using CFrame:
1 | while true do |
2 | script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(. 002 ,. 005 ,. 009 ) |
3 | wait(. 001 ) |
4 | end |
Here is the code using Vector3:
01 | platform = script.Parent |
02 | a = 0 |
03 | b = 0 |
04 | c = 0 |
05 |
06 | while true do |
07 | platform.Rotation = Vector 3. new(a, b, c) |
08 | wait( 0.01 ) |
09 | a = a + 0.1 |
10 | b = b + 0.2 |
11 | c = c + 0.15 |
12 | end |
Any suggestions on how I can make the player rotate with the platform?
To make a player stick onto a moving part, whether that be a spinning or just translating, you should use a BodyMover.
BodyMovers are a class of instances in roblox that exert forces on baseparts. There are Seven types of BodyMovers currently in roblox BodyForce
,BodyPosition
, and BodyVelocity
for translation, BodyThrust
,BodyGyro
, and BodyAngularVelocity
for rotation, and the odd one out, RocketPropulsion
To achieve what you are trying to do, you can use the BodyGyro to maintain a constant rotation.
One thing to keep in mind is that CFrame.fromEulerAngles and CFrame.Angles uses radians instead of degrees. Generally, a degree is pi/180 radians, as a pi radians is 180 degrees
01 | local Gyro = script.Parent.BodyGyro |
02 | local ang = 0 |
03 | Torque = Vector 3. new( math.huge , math.huge , math.huge ) |
04 |
05 | Gyro.MaxTorque = d |
06 |
07 | while true do |
08 | Gyro.CFrame = Gyro.CFrame * CFrame.Angles( 0 ,math.rad(ang/ 10 ), 0 ) |
09 | print (Gyro.CFrame) |
10 | ang = ang + 1 |
11 | wait( 0.1 ) |
12 | end |
Now that you have a rotating part, you might want to make it float.
To do this, you can use a BodyPosition
.
Ex:
1 | local BodyPos = script.Parent.BodyPosition |
2 |
3 | BodyPos.MaxForce = Vector 3. new( math.huge , math.huge , math.huge ) |
4 | BodyPos.Position = Vectore.new(x,y,z) --placeholder coordinates |
Possibility 1: Using lookVectors
My first suggestion is to use lookVectors to push the player along the face they are standing on.
01 | local player = game.Players.LocalPlayer |
02 | local part = workspace.Part |
03 |
04 | part.Touched:Connect( function () |
05 | player.Character:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + (part.CFrame.LookVector * Vector 3. new( 0.05 , 0 , 0 ))) |
06 | end ) |
07 |
08 | while true do |
09 | part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(. 002 ,. 005 ,. 009 ) |
10 | wait(. 001 ) |
11 | end |
Possibility 2: Using Welds
My second thought is to try Welds, which will guarantee the player is moving with the part's rotation, but must be broken / removed by another function (as the player wlll be unable to move)
01 | local player = game.Players.LocalPlayer |
02 | local part = workspace.Part |
03 |
04 | part.Touched:Connect( function () |
05 | local weld = Instance.new( "Weld" , player.Character.HumanoidRootPart) |
06 | weld.Part 0 = player.Character.HumanoidRootPart |
07 | weld.Part 1 = part |
08 | weld.C 0 = CFrame.new( 0 , - 5 , 0 ) |
09 | end ) |
10 |
11 | while true do |
12 | part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(. 002 ,. 005 ,. 009 ) |
13 | wait(. 001 ) |
14 | end |
I'll be happy to answer any questions or concerns regarding these methods.
EDIT: Comment, to call the player in the server, you need to activate a touch-based detectable event (usu. w/ ClickDetectors or .Touched)
Server Variant of lookVector
01 | local part = script.Parent |
02 |
03 | part.Touched:Connect( function (hit) |
04 | if hit.Parent:FindFirstChild( "Humanoid" ) ~ = nil then |
05 | local character = hit.Parent |
06 | character:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame + (part.CFrame.LookVector * Vector 3. new( 0.05 , 0 , 0 ))) |
07 | end |
08 | end ) |
09 |
10 | while true do |
11 | part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(. 002 ,. 005 ,. 009 ) |
12 | wait(. 001 ) |
13 | end |
However, since this doesn't work as smoothly as the Local Variant I suggest using the first code listed above instead (placed in a LocalScript under StarterGui)