Half of my script is working

--------------------------------
--bob up and down part
local amplitude = 0.4
local magnitude = 2

local part = script.Parent
local defaultCFrame = part.CFrame

game:GetService("RunService").Stepped:Connect(function()
	part.CFrame = defaultCFrame + Vector3.new(0, amplitude*math.sin(tick()*math.pi/magnitude), 0)
end)

-----------------------------------
--rotation part
while true do
	script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.04,0)
	wait(0.01)
end

what im trying to do is make a part bob up and down while rotating but it’s ONLY bobbing up and down, not rotating. i’ve tried putting them into two different scripts but that still didn’t work. any suggestions on how i could fix this

why don’t you put them both in the same loop? maybe add a task.spawn

what does task.spawn do???

say you want to make something happen that requires waiting but you want the script to continue on. Thats what task.spawn() is for. Here’s an example:

local part1 = workspace.Part
local part2 = workspace.Part2
task.spawn(function()
    part1.Transparency = 1
    task.wait(10)
    part1.Transparency = 0
end)
part2.Transparency =1
task.wait(1)
part2.Transparency =0

In the example the first function does not effect the rest of the code with the task.wait(). I suck at explaining so here’s a link Roblox Globals – just scroll down to spawn

so like this?

--------------------------------
--bob up and down part
local amplitude = 0.4
local magnitude = 2

local part = script.Parent
local defaultCFrame = part.CFrame

game:GetService("RunService").Stepped:Connect(function()
	part.CFrame = defaultCFrame + Vector3.new(0, amplitude*math.sin(tick()*math.pi/magnitude), 0)
end)

-----------------------------------
--rotation part
task.spawn(function()
while true do
	script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.04,0)
	wait(0.01)
end
end)

now move it before the RunService function

--------------------------------
--bob up and down part
local amplitude = 0.4
local magnitude = 2

local part = script.Parent
local defaultCFrame = part.CFrame
--rotation part
task.spawn(function()
while true do
	script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.04,0)
	wait(0.01)
end
end)
game:GetService("RunService").Stepped:Connect(function()
	part.CFrame = defaultCFrame + Vector3.new(0, amplitude*math.sin(tick()*math.pi/magnitude), 0)
end)


moving it before runservice didn’t do anything .-.

My bad, the first script should of worked perfectly fine, its the fact that you are changing the CFrame in both loops, try setting the defaultCframe every loop in the RunService and working off that

this should work:

--------------------------------
--bob up and down part
local amplitude = 0.4
local magnitude = 2

local part = script.Parent
local defaultCFrame = part.CFrame
--rotation part
task.spawn(function()
	while true do
		script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.04,0)
		wait(0.01)
	end
end)
game:GetService("RunService").Stepped:Connect(function()
	defaultCFrame = part.CFrame
	part.CFrame = defaultCFrame + Vector3.new(0, amplitude*math.sin(tick()*math.pi/magnitude), 0)
end)

this works, although it totally breaks the bobbing in a way. so before we fixed it, it bobbed how i wanted it to. now it bobs up and down ~100 studs and changing the amplitute and magnitude variables don’t do anything

Put this in the same script:

--//Services
local RunService = game:GetService("RunService")

--//Variables
local part = script.Parent
local defaultCFrame = part.CFrame

--//Controls
local amplitude = 0.4
local magnitude = 2
local rotation = CFrame.fromEulerAnglesXYZ(0, 0.04, 0)

--//Functions
coroutine.wrap(function()
	while part do
		part.CFrame *= rotation
		
		task.wait(0.01)
	end
end)()

RunService.Stepped:Connect(function()
	part.CFrame = defaultCFrame + Vector3.new(0, amplitude * math.sin(tick() * math.pi/magnitude), 0)
end)

changing the amplitude and magnitude still don’t do anything. it still goes up and down the same height, the same speed.

local amplitude = 0.4
local magnitude = 2

local part = script.Parent
local defaultCFrame = part.CFrame
local rotation

task.spawn(function()
	local i = 0.04
	while true do
		rotation = CFrame.fromEulerAnglesXYZ(0,i,0)
		wait(0.01)
		i += 0.04
	end
end)
game:GetService("RunService").Stepped:Connect(function()
	part.CFrame = defaultCFrame*rotation + Vector3.new(0, amplitude*math.sin(tick()*math.pi/(magnitude)), 0)
	
end)

does that work?

yesssssss finally thanks brooooo

1 Like