Need help with touched event

Hi, Scripters! I’m currently trying to make a script that creates a new Ball every time the function is run. However, I want to be able to do 3 things: Put each ball in a folder when it is created, Make sure that only a certain amount of balls can be in the game at one time, and be able to connect a touched function to each ball.

The issue I’m forgoing is that I don’t know how to add instances to folders in a script, also meaning I don’t know how to tell how many balls are in the folder, and I need someone to tell me how I can connect a touched function to a ball that is created.

Thanks to anyone that can help!

function NewBall()
	local Ball = Instance.new("Part")
	local Num1 = math.random(200,255)
	local Num2 = math.random(100,255)
	local Num3 = math.random(1,255)
	local Loc1 = math.random(-50,50)
	local Loc2 = math.random(-50,50)
	local size = math.random(1, 5)
	Balls.Shape = Enum.PartType.Ball
	Balls.Size = Vector3.new(size,size,size)
	Balls.Transparency = 0.5
	Balls.Color = Color3.new(Num1, Num2, Num3)
	Balls.Material = Enum.Material.SmoothPlastic
	Balls.Position = Vector3.new(Loc1, 3, Loc2)
	Balls.Anchored = true
	Balls.CanCollide = false
	if Balls.Size.X >= 4 then
		Balls.Material = Enum.Material.Neon
	end
	if Balls.Size.X < 4 then
		Balls.Size = Vector3.new(2, 2, 2)
	end
	Balls.Parent = game.Workspace
	return Balls
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Instead of Ball.Parent = game.Workspace you should make Ball.Parent = the location of the folder in your workspace.

Thanks! That helps my first issue :slight_smile:

Yeah like the person said above change the line

Balls.Parent = game.Workspace

To something like

Balls.Parent = game.Workspace.WhateverFolderName

For this you can check the number of balls in that folder that you created by doing:

if #game.Workspace.WhateverFolderName:GetChildren() <= 50 then -- 50 would be your max amount of balls so you can change that.
 -- Run your function to create a new ball
end

You can connect touched events by doing the following, this script would be added to the end of your function before the return Balls

Balls.Touched:Connect(function()
  -- Do whatever code you want when the ball is touched
end)

Thanks mate! This will help me a TON!

1 Like

Now I have one more question:

Let’s say I wanted to have 200 balls in my game at once. Right here I am creating the balls until there are 200. My script will make it so that when one ball is touched, it will be destroyed and another ball will spawn in replacement of that. Could you solve this problem as well?

function NewBall()
	local Ball = Instance.new("Part")
	local Num1 = math.random(200,255)
	local Num2 = math.random(100,255)
	local Num3 = math.random(1,255)
	local Loc1 = math.random(-50,50)
	local Loc2 = math.random(-50,50)
	local size = math.random(1, 5)
	Ball.Shape = Enum.PartType.Ball
	Ball.Size = Vector3.new(size,size,size)
	Ball.Transparency = 0.5
	Ball.Color = Color3.new(Num1, Num2, Num3)
	Ball.Material = Enum.Material.SmoothPlastic
	Ball.Position = Vector3.new(Loc1, 3, Loc2)
	Ball.Anchored = true
	Ball.CanCollide = false
	if Ball.Size.X >= 4 then
		Ball.Material = Enum.Material.Neon
	end
	if Ball.Size.X < 4 then
		Ball.Size = Vector3.new(2, 2, 2)
	end
	Ball.Parent = game.Workspace.Ball
	Ball.Touched:Connect(function()
		Ball:Destroy()
	end)
	return Ball
end

while true do
	if game.Workspace.Balls:GetChildren() <= 200 then
		
	end
	NewBall()
end

I guess you could do something like this:
Replace this code:

Ball.Touched:Connect(function()
	Ball:Destroy()
end)

with

Ball.Touched:Connect(function()
	Ball:Destroy()
    NewBall()
end)

So you are creating a new ball whenever one gets destroyed

Also as a note:
You need to change this line:

while true do
	if game.Workspace.Balls:GetChildren() <= 200 then
		
	end
	NewBall()
end

to this:

while true do
	if game.Workspace.Balls:GetChildren() <= 200 then
		NewBall()
	end
end

The point of the if statement is to stop the function from running if there is too many balls

1 Like

How could this make sure that 200 balls would remain in the folder? If I kept the while loop going, it would just go on forever.

Oops, I didn’t read the bottom part. My bad!

1 Like