Need help on making a group floating script

Hello!

I have made a script that is supposed to make any group “float” from its original position.

( Read my reply below for updated info on this issue! )

I tried doing this from the group WorldPivot.Position and adding a Vector3 Value, but this failed.

I thought this was something to do with not being able to use Vector3s or something, so I went to the DevHub to learn more about CFrames as I had not used them before. This also failed.

Link to DevHub API on CFrames

I understand that CFrame.New() is a Vector3 value, I just thought that using Vector3.new would be different somehow.

Code Here
local ChocolateMilk = script.Parent

local ShouldMilkFloat = true

local ChocolateMilksNormalPostion = ChocolateMilk.WorldPivot.Position
local MaxHighYPostion = ChocolateMilk.WorldPivot.Position + CFrame.new(0,1,0)
local MaxLowYPostion = ChocolateMilk.WorldPivot.Position + CFrame.new(0,-1,0)

while ShouldMilkFloat == true do
	wait()
	
	if ChocolateMilk.WorldPivot.Position >= MaxHighYPostion then -- Make milk go down if too high
		wait()
		ChocolateMilk.WorldPivot.Position = ChocolateMilk.WorldPivot.Position - CFrame.new(0,0.1,0)
	end
	
	if ChocolateMilk.WorldPivot.Position <= MaxLowYPostion then -- Make milk go up if too low
		wait()
		ChocolateMilk.WorldPivot.Position = ChocolateMilk.WorldPivot.Position + CFrame.new(0,0.1,0)
	end
	
end

Here is the error I get when I run it:

What is wrong with my code? And if this doesn’t work, how could I achive my plan. ( Making a group “float” up and down from its original position )

I fixed the issue. As the error said, I was trying to compare two Vector3 values. I changed things around and the code now works, but the group still doesn’t move.

New Code
local ChocolateMilk = script.Parent

local ShouldMilkFloat = true

local ChocolateMilksNormalYPostion = ChocolateMilk.WorldPivot.Position.Y
local MaxHighYPostion = ChocolateMilksNormalYPostion + 1 
local MaxLowYPostion = ChocolateMilksNormalYPostion - 1

while ShouldMilkFloat == true do
	wait()
	
	if ChocolateMilk.WorldPivot.Position.Y > MaxHighYPostion then -- Make milk go down if too high
		wait()
		ChocolateMilk.WorldPivot.Position.Y = ChocolateMilk.WorldPivot.Position.Y - 1
	end

	if ChocolateMilk.WorldPivot.Position.Y < MaxLowYPostion then -- Make milk go up if too low
		wait()
		ChocolateMilk.WorldPivot.Position.Y = ChocolateMilk.WorldPivot.Position + 1
	end
	
end
1 Like

Okay, try using TweenService.
Make a new TweenInfo and set the properties and for the reverse property, set it to true.
Then create a new var for the newposition like
newPosition = Veftor3.new(0,5,0) --it moves up and down

Then play the animation using TweenService.

Also, when I referenced the API for WorldPivot.Position, it says the World.Pivot.Position cannot be changed or accessed thru Script and the value can b changed only thru the properties tab and if u try to change it in-script it will return an error. So ye, the mite b wt is causing the issue.

P.S: I tried ur code in studio and apparently when u try to access WorldPivot.Position it returns an error.

1 Like