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.
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.
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 ._.
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)
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.
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)
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)
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
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?
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.
– Set up the player’s data
if not LocalPlayer.Data then
local playerData = Instance.new(“ObjectValue”)
playerData.Name = “Data”
playerData.Parent = LocalPlayer
– 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 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