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!