Issues with ScreenGUI MiniGame

What I’m trying to do is make it so that the player can’t move when the screenGui is on their screen. I’ve tried disabling the character and locking their playerGui but that doesn’t work.
I’d also like to be able to have the strawberries regenerate continuously.

Try:
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local RunService = game:GetService(“RunService”)
local Players = game:GetService(“Players”)
local LocalPlayer = Players.LocalPlayer
local UserInputService = game:GetService(“UserInputService”)
local GuiService = game:GetService(“GuiService”)

local function newCharacter()
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
return character
end

local function newStrawberry(parent)
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 = parent
return strawberry
end

– Function to move the character to the left
local function moveLeft(character)
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 move

I’ve added

table.remove(strawberries, strawberry)

to the end of my entire script but it still doesn’t work… unless I’m not putting it into the correct place within the script?

I’m not sure what you’re trying to reply — sorry.

Then my last option really is controling them with a number value in repStorage

1 Like

I guess… just to make sure — am I doing this correctly?

local function resetGame()
	-- Reset the game and add new strawberries
	for i = 1, 5 do
		task.wait(5)
	--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)

-- 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()
		resetGame()
		
	end)
end)

table.remove(strawberries, strawberry)

I’ve also tried this:

local function resetGame()
	-- Reset the game and add new strawberries
	for i = 1, 5 do
		task.wait(5)
	--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()
	table.remove(strawberries, strawberry)
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()
		resetGame()
		
	end)
end)

I guess, I’m not really sure where to put the table.remove(strawberries, strawberry) code

Table.remove you could put after the task.wait() inside the resetGame function

Else, when i get home i will rewrite your script

1 Like

Thank you for taking the time to help me, it means a lot.

Here’s the entire script as I currently have it:

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.BackgroundTransparency = 1 --added
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.BackgroundTransparency = 1 --added
	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
local function resetGame()
	-- Reset the game and add new strawberries
	for i = 1, 5 do
		task.wait(5)
	--while #strawberries < 5 do
		local strawberry = Instance.new("ImageLabel")
		strawberry.Name = "Strawberry"
		strawberry.Size = UDim2.new(0, 25, 0, 25)
		strawberry.BackgroundTransparency = 1 --added
		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)

-- 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()
		resetGame()
		
	end)
end)

In this script you repeat same function, for example when you cloning strawberries you just need to write it in one function, or why you use runservice for the same function, you start creating strawberries before clicking the play button. And don’t use localscript on any data you want to save, it won’t save. And also creating a play button seems unnecessary to me, but that’s up to everyone.

Hi! Just letting you know that I’ve made the game work & while it isn’t perfect — it’s good enough!

Here’s a link to the final product: My Melody's Strawberry Catcher - Roblox :sparkling_heart:

Here’s an update on the mini-game!

Thats great news! Good luck on the carrer out there!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.