Issues with ScreenGUI MiniGame

  1. What do you want to achieve?
    I’m trying to make a minigame within a ScreenGUI & I’ve written out the code; however, it’s still not working the way I want it to.

  2. What is the issue?
    I’d like to have the strawberries regenerate continuously & have the player locked in place when the screenGUI is on their screen.

  3. What solutions have you tried so far?
    I have looked on the DevForum for similar topics related to what I’m trying to accomplish but I haven’t been able to find any ._.

In addition, there are no output errors!

Screenshot

Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")

-- Create the character object and add it to the screengui
local character = Instance.new("ImageLabel")
character.Name = "Character"
character.Size = UDim2.new(0, 50, 0, 50)
character.Position = UDim2.new(0.5, -25, 0.9, -50)
character.Image = "rbxassetid://10666795198" -- Replace with the ID of the image you want to use for the character
character.Parent = script.Parent

-- Create the strawberry objects and add them to the screengui
local strawberries = {}
for i = 1, 5 do
	local strawberry = Instance.new("ImageLabel")
	strawberry.Name = "Strawberry"
	strawberry.Size = UDim2.new(0, 25, 0, 25)
	strawberry.Position = UDim2.new(math.random(), -25, -0.1, 0)
	strawberry.Image = "rbxassetid://11807642473" -- Replace with the ID of the image you want to use for the strawberries
	strawberry.Parent = script.Parent
	table.insert(strawberries, strawberry)
end

-- Function to move the character to the left
local function moveLeft()
	local currentPos = character.Position
	character.Position = UDim2.new(currentPos.X.Scale, currentPos.X.Offset - 5, currentPos.Y.Scale, currentPos.Y.Offset)
end

-- Function to move the character to the right
local function moveRight()
	local currentPos = character.Position
	character.Position = UDim2.new(currentPos.X.Scale, currentPos.X.Offset + 5, currentPos.Y.Scale, currentPos.Y.Offset)
end

-- Function to move the strawberries down
local function moveStrawberries()
	for i, strawberry in ipairs(strawberries) do
		local currentPos = strawberry.Position
		strawberry.Position = UDim2.new(currentPos.X.Scale, currentPos.X.Offset, currentPos.Y.Scale, currentPos.Y.Offset + 5)
	end
end

-- Function to check for collisions between the character and the strawberries
local function checkCollisions()
	for i, strawberry in ipairs(strawberries) do
		local characterPos = character.AbsolutePosition
		local characterSize = character.AbsoluteSize
		local strawberryPos = strawberry.AbsolutePosition
		local strawberrySize = strawberry.AbsoluteSize
		if characterPos.X < strawberryPos.X + strawberrySize.X and
			characterPos.X + characterSize.X > strawberryPos.X and
			characterPos.Y < strawberryPos.Y + strawberrySize.Y and
			characterSize.Y + characterPos.Y > strawberryPos.Y then
			-- Collision detected! Remove the strawberry and give the player a point
			strawberry:Destroy()
			table.remove(strawberries, i)
			LocalPlayer.Data.Points.Value = LocalPlayer.Data.Points.Value + 1
		end
	end
end

-- Function to reset the game when all strawberries have been collected
local function checkWin()
	if #strawberries == 0 then
		-- All strawberries have been collected, so reset the game
		for i = 1, 5 do
			local strawberry = Instance.new("ImageLabel")
			strawberry.Name = "Strawberry"
			strawberry.Size = UDim2.new(0, 25, 0, 25)
			strawberry.Position = UDim2.new(math.random(), -25, -0.1, 0)
			strawberry.Image = "rbxassetid://11807642473" -- Replace with the ID of the image you want to use for the strawberries
			strawberry.Parent = script.Parent
			table.insert(strawberries, strawberry)
		end
	end
end

-- Connect the character movement functions to player input events
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.A then
		moveLeft()
	elseif input.KeyCode == Enum.KeyCode.D then
		moveRight()
	end
end)

-- Create the play button and start the game when it is clicked
local playButton = Instance.new("TextButton")
playButton.Name = "PlayButton"
playButton.Text = "Play"
playButton.Size = UDim2.new(0, 50, 0, 50)
playButton.Position = UDim2.new(0.5, -25, 0.5, -25)
playButton.Parent = script.Parent

playButton.MouseButton1Click:Connect(function()
	-- Start the game
	playButton.Visible = false
	RunService.RenderStepped:Connect(function()
		moveStrawberries()
		checkCollisions()
		checkWin()
	end)
end)

What exactly do you want to do and what is the issue, specifically?

1 Like

I believe I’ve fixed the original issues I was having; however, I’m having more issues now…

Now I’m getting an absolute TON of strawberries on the screen which is not very desirable… I believe that issue is related to this part in the script here:

local function resetGame()
	-- Reset the game and add new strawberries
	for i = 1, 5 do
		local strawberry = Instance.new("ImageLabel")
		strawberry.Name = "Strawberry"
		strawberry.Size = UDim2.new(0, 25, 0, 25)
		strawberry.Position = UDim2.new(math.random(), -25, -0.1, 0)
		strawberry.Image = "rbxassetid://11807642473" -- Replace with the ID of the image you want to use for the strawberries
		strawberry.Parent = script.Parent
		table.insert(strawberries, strawberry)
	end
end

-- Continuously reset the game and add new strawberries
RunService.RenderStepped:Connect(function()
	wait(10)
	resetGame()
end)

Also, I’d like to make it so the player can’t move when they’ve already pressed play.

Here’s a photo of the current situation:

And a video (once it’s done uploading you’ll be able to view it)
https://vimeo.com/787508185

Hello, have you tried using task.wait(number) inside the function?

1 Like

Or, perhaps you could place a limiter on the ammount of strawberrys on the screen with a number value

1 Like

Thank you!

I’ve added a ‘task.wait(5)’ function & it works at first, but then after it spams the strawberries again.

local function resetGame()
	-- Reset the game and add new strawberries
	for i = 1, 5 do
		task.wait(5)
		local strawberry = Instance.new("ImageLabel")
		strawberry.Name = "Strawberry"
		strawberry.Size = UDim2.new(0, 25, 0, 25)
		strawberry.Position = UDim2.new(math.random(), -25, -0.1, 0)
		strawberry.Image = "rbxassetid://11807642473" -- Replace with the ID of the image you want to use for the strawberries
		strawberry.Parent = script.Parent
		table.insert(strawberries, strawberry)
		
	end
end

-- Continuously reset the game and add new strawberries
RunService.RenderStepped:Connect(function()
	resetGame()
end)

You need to use a normal while loop, you are currently spawning strawberries every frame. I don’t think waiting every RenderStepped is what you want.

1 Like

I’ve also tried that, but for some reason then, the strawberries don’t respawn after they move off of the screen.

local function resetGame()
	-- Reset the game and add new strawberries
	while #strawberries < 5 do
		local strawberry = Instance.new("ImageLabel")
		strawberry.Name = "Strawberry"
		strawberry.Size = UDim2.new(0, 25, 0, 25)
		strawberry.Position = UDim2.new(math.random(), -25, -0.1, 0)
		strawberry.Image = "rbxassetid://11807642473" -- Replace with the ID of the image you want to use for the strawberries
		strawberry.Parent = script.Parent
		table.insert(strawberries, strawberry)
		
	end
end

-- Continuously reset the game and add new strawberries
RunService.RenderStepped:Connect(function()
	resetGame()
end)

Thank you for your reply, but how do I exactly implement this ‘while’ loop within this script?

Well the last option i could think is put a while wait loop and check with the limiter every strawberry placed, after the limit is reached you could do a break function

1 Like

Oh, they dont appear beacuse you dont delete them from the table after the function is complete

1 Like

I see! How can I fix this issue?

Replace


RunService.RenderStepped:Connect(function()
	resetGame()
end)

With


while true do
	resetGame()

	task.wait(10)

end

Now the game won’t start & the ‘Play’ button won’t appear.

Try doing a function when the player clicks the image or whatever you want to do with the image, there is a function table.remove

Or you could the same and more simple with a number value controling the ammount of strawberrys.

So instead of inserting in the table you could add in the value

And you can check the limit with the value

1 Like

Is there something I can add to the ‘resetGame function’ so that after a certain number of seconds (for example, 5) the strawberries on the screen will despawn & new strawberries will appear?

Then you might want to use task.spawn or putting the while loop at the very bottom of the script.

task.spawn(function()
while true do
resetGame()
task.wait(10)
end

end)

1 Like

I’ve added the code you’ve provided to the end of the script; however, the same thing keeps happening — the first 5 strawberries spawn, and then the massive amounts spawn a few seconds afterward.

Perhaps a table.remove() at the end with your values used

1 Like

– Set up the player’s data
if not LocalPlayer.Data then
local playerData = Instance.new(“ObjectValue”)
playerData.Name = “Data”
playerData.Parent = LocalPlayer

local playerPoints = Instance.new("IntValue")
playerPoints.Name = "Points"
playerPoints.Value = 0
playerPoints.Parent = playerData

end

– Connect the player’s points value to the screengui
LocalPlayer.Data.Points.Changed:Connect(function(newPoints)
script.Parent.Points.Text = tostring(newPoints)
end)

– Create the screengui and add it to the local player’s player gui
local screenGui = Instance.new(“ScreenGui”)
screenGui.Name = “StrawberryGameGui”
screenGui.Parent = LocalPlayer.PlayerGui

local screenGuiFrame = Instance.new(“Frame”)
screenGuiFrame.Name = “Frame”
screenGuiFrame.Size = UDim2.new(0, 200, 0, 200)
screenGuiFrame.Position = UDim2.new(0.5, -100, 0.5, -100)
screenGuiFrame.Parent = screenGui

local pointsTextLabel = Instance.new(“TextLabel”)
pointsTextLabel.Name = “Points”
pointsTextLabel.Text = “0”
pointsTextLabel.Size = UDim2.new(0, 100, 0, 50)
pointsTextLabel.Position = UDim2.new(0.5, -50, 0, 0)
pointsTextLabel.TextWrapped = true
pointsTextLabel.Parent = screenGuiFrame

local instructionsTextLabel = Instance.new(“TextLabel”)
instructionsTextLabel.Name = “Instructions”
instructionsTextLabel.Text = “Press A to move left and press D to move right.”
instructionsTextLabel.Size = UDim2.new(0, 100, 0, 50)
instructionsTextLabel.Position = UDim2.new(0.5, -50, 0, 50)
instructionsTextLabel.TextWra