Part Will Not Stop Spinning

I have created a really simple game intended for my 2 year old, where the user either presses a ScreenGui button, presses E on their keyboard, or presses X (square for PlayStation) on their controller to active a function in which a part spins until the PrimaryPart’s Y-Orientation is 90, and then displays an animal as well as plays the sound it makes, and then spins until the PrimaryPart’s Y-Orientation is -90. After this the user would active the function again to see a different animal and hear a different sound.

Here is the code for the function, stored in a localscript in the StarterPlayer’s StarterPlayerScripts folder:

local sound = game:GetService("SoundService")
local userInput = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local lastKeyPressed = nil

local animal1 = rs.Folder.Animal1
local animal2 = rs.Folder.Animal2
local part = workspace.Part

local player = game.Players.LocalPlayer

local screenGui = player.PlayerGui:WaitForChild("ScreenGui")
local button = screenGui.TextButton
local label = screenGui.TextLabel

button.Visible = true
lable.Visible = false

local debounce = false

local function function1()
	if debounce == false then
		debounce = true

		local random = math.random(1, 2)
		button.Visible = false

		repeat
			wait(.1)
			part:PivotTo(part:GetPivot() * CFrame.Angles(math.rad(0+5), 0, 0))
		until part.PrimaryPart.Orientation.Y == 90

		if random == 1 then
			animal1.Parent = workspace
			label.Visible = true
			lable.Text = "Animal1"
			wait(.25)
			sound.Folder.Animal1Sound:Play()
			if sound.Folder.Animal1Sound.Playing == true then
				sound.Folder.Animal1.Ended:Wait()
			end
			wait(5)
			label.Visible = false
			animal1.Parent = replicatedStorage
		end

		if random == 2 then
			animal2.Parent = workspace
			label.Visible = true
			lable.Text = "Animal2"
			wait(.25)
			sound.Folder.Animal2Sound:Play()
			if sound.Folder.Animal2Sound.Playing == true then
				sound.Folder.Animal2Sound.Ended:Wait()
			end
			wait(5)
			label.Visible = false
			animal2.Parent = replicatedStorage
		end

		repeat
			wait(.1)
			part:PivotTo(part:GetPivot() * CFrame.Angles(math.rad(0+5), 0, 0))
		until part.PrimaryPart.Orientation.Y == -90

		button.Visible = true
		debounce = false
	end
end

userInput.InputBegan:Connect(function(key, processed)
	if key.UserInputType == Enum.UserInputType.Gamepad1 then
		lastKeyPressed = key.KeyCode
		if key.KeyCode == Enum.Keycode.ButtonX then
			function1()
		end
	end

	if key.UserInputType == Enum.UserInputType.Keyboard then
		lastKeyPressed = key.KeyCode
		if key.KeyCode == Enum.Keycode.E then
			function1()
		end
	end
end)

button.MouseButton1Down:Connect(function(clicked)
	function1()
end)

It’s working

AnimalSpinner-Working.wmv (2.9 MB)

It works, but after a while it breaks like this:

AnimalSpinner-Broken.wmv (1.0 MB)

The game can be found here:
Animal Spinner

The Output console does not show any errors, and I have tried to find a solution to this issue on the forums to no avail. I am unsure if it is a error in the scripting or if it is a bug.

If anyone could help, I would greatly appreciate it as my daughter absolutely loves what I have made for her, but it gets tiresome having to restart it. Thanks!

This may be too focused. Try >89 or maybe >89.9
At that point you could just state where it should be after leaving the loop.

I appreciate your help with my issue, however I was able to fix the issue by just having it go back the way it came. I couldn’t figure out how to set the parts after the loop without them flipping this way and that, I believe this to be on my behalf and not that you were wrong by any means as I am sure it would have worked if I could do it; so I will mark your response as the answer.

If the 1st part of this worked (and it looks like it did) the >89, you may not need to do the 2nd part.
You could also try this …

local part = script.Parent -- however you get to the main part
local tweenService = game:GetService("TweenService")
local initialRotation = part.CFrame
local endRotation1 = initialRotation * CFrame.Angles(0, math.rad(179.99), 0)
local endRotation2 = initialRotation * CFrame.Angles(0, 0, 0)
local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Linear)
local tween1 = tweenService:Create(part, tweenInfo, {CFrame = endRotation1})
local tween2 = tweenService:Create(part, tweenInfo, {CFrame = endRotation2})

--this next part is not how you would set it up at all 
--do it like you're doing using tween1 or 2 vs using that loop

task.wait(5)
tween1:Play() 
task.wait(4) 
tween2:Play()

-- they click use tween1:Play() 
-- they click again use tween2:Play()
-- these don't need to be re-defined. 

This is pretty smooth the 3 is defining speed
If you use a 180 it will keep rotating the same way sometimes.
Sorry, I found this a bit hard to explain :rofl:

1 Like

That worked! Can’t believe I completely forgot about tweens :rofl: Thanks so much :smile:

1 Like