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

How can I make a rotating block that the player sticks to?

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:

while true do
	script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(.002,.005,.009)
	wait(.001)
end

Here is the code using Vector3:

platform = script.Parent
a = 0
b = 0
c = 0

while true do
	platform.Rotation = Vector3.new(a, b, c)
	wait(0.01)
	a = a + 0.1
	b = b + 0.2
	c = c + 0.15
end

Any suggestions on how I can make the player rotate with the platform?

2 comments

0

just edited my answer to include making the part float

0

I added a comment, and adjusted the scripts accordingly, it will index the player correctly now, though make sure that the script / localscript is in the correct parent (workspace / serverscriptservice or startergui)

2 answers

✓ Accepted answer

To make a player stick onto a moving part, whether that be a spinning or just translating, you should use a BodyMover.


BodyMovers


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


Application


local Gyro = script.Parent.BodyGyro
local  ang = 0
Torque = Vector3.new(math.huge,math.huge,math.huge)

Gyro.MaxTorque = d 

while true do
	Gyro.CFrame = Gyro.CFrame * CFrame.Angles(0,math.rad(ang/10),0)
	print(Gyro.CFrame)
	ang = ang + 1
	wait(0.1)
end

Making a floating part


Now that you have a rotating part, you might want to make it float.

To do this, you can use a BodyPosition.

Ex:

local BodyPos = script.Parent.BodyPosition

BodyPos.MaxForce =  Vector3.new(math.huge,math.huge,math.huge)
BodyPos.Position = Vectore.new(x,y,z) --placeholder coordinates

Reference links


BodyMovers

BodyGyro

Radians

2 comments

0

Thank you, worked like a charm. However I have a quick question (which this is probably a simple answer to). How can I make the block stay in place in midair while also rotating?

0

use a bodyposition

Possibility 1: Using lookVectors

My first suggestion is to use lookVectors to push the player along the face they are standing on.

local player = game.Players.LocalPlayer
local part = workspace.Part

part.Touched:Connect(function()
	player.Character:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + (part.CFrame.LookVector * Vector3.new(0.05, 0, 0)))
end)

while true do
	part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(.002,.005,.009)
	wait(.001)
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)

local player = game.Players.LocalPlayer
local part = workspace.Part

part.Touched:Connect(function()
	local weld = Instance.new("Weld", player.Character.HumanoidRootPart)
	weld.Part0 = player.Character.HumanoidRootPart
	weld.Part1 = part
	weld.C0 = CFrame.new(0, -5, 0)
end)

while true do
	part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(.002,.005,.009)
	wait(.001)
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

local part = script.Parent

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		local character = hit.Parent
		character:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame + (part.CFrame.LookVector * Vector3.new(0.05, 0, 0)))
	end
end)

while true do
	part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(.002,.005,.009)
	wait(.001)
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)

4 comments

0

Moving the player like that on a rotating part has the possibility of having the player getting stuck in the part itself

0

with the weld, yes, thats why i said its a possibility

0

I like the idea of using lookvVectors but it threw up an error when a player touches the block stating in line 5, "attempt to index upvalue 'player' (a nil value)." I am pretty new to coding and I can't figure out what this means or how to fix it.

0

@ejones, is your script local or on the server? If its on the server, the example i have is for local, so you'd have to adjust slightly to check which player is touching the part, however, I would suggest the local version since it runs better

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