Detecting a ball inside of a part

I’m trying to make it so when the ball stops, whichever color it is on, it will detect the ball on that color.

I’ve tested with print commands, and it seems like the remote event isn’t even going through.

I’ve been trying this for a while and it’s just not working, any solutions?

local db = false

game.Workspace.Machines.Machine1.Button.ProximityPrompt.Triggered:Connect(function()
	
	if db == false then
		
		game.Workspace.Machines.Machine1.Button.ProximityPrompt.Enabled = false
	
	local ball = game.ReplicatedStorage.Balls.Ball1:Clone()
	
		print("Worked")
	ball.Parent = game.Workspace
	ball.Transparency = 0
	ball.CanCollide = true
	ball.Anchored = false
		
		repeat
			
			wait()
			
		until ball.AssemblyLinearVelocity.Magnitude < .25
		
		ball.Anchored = true
		ball.BrickColor = BrickColor.new(1, 0, 0.0156863)
		
		local player = game.Players:GetPlayerFromCharacter(script.Parent)
		
		game.ReplicatedStorage.RemoteEvents.Check1:FireClient(player)
				
		wait(1)
		
		ball:Destroy()
		
		db = false
		
		game.Workspace.Machines.Machine1.Button.ProximityPrompt.Enabled = true
		
	end
end)

Green part hitbox script:

game.ReplicatedStorage.RemoteEvents.Check1.OnClientEvent:Connect(function()
	
	print("called")
	
	script.Parent.Touched:Connect(function(hit)
		
		print("detected")
		
		if hit.Name == "Ball1" then
			
			print("Win")
			
			game.ReplicatedStorage.RemoteEvents.Win:FireServer()
			
		end
		
	end)
	
end)
1 Like

when ball stops, call ball:GetTouchingParts()

From the documentation hub:

A LocalScript will only run Lua code if it is a descendant of one of the following objects:

I see your “Green part hitbox script” which I assume is a LocalScript, is inside a part because I see script.Parent.Touched.

1 Like

when ball stops, call
ball:GetTouchingParts()

repeat
	wait()
until ball.AssemblyLinearVelocity.Magnitude < .25
		
ball.Anchored = true
ball.BrickColor = BrickColor.new(1, 0, 0.0156863)

local parts = ball:GetTouchingParts()  -- here
for i, part in pairs(parts) do
   if part.Name == "GreenZone" then
	  print("Win")
	  game.ReplicatedStorage.RemoteEvents.Win:FireServer()
   elseif part.Name == "RedZone" then
      -- lose code here
   end
end

wait(1)
ball:Destroy()
1 Like

it still doesnt seem to be working, like the print “Win” isnt showing up

local db = false
local player = game.Players:GetPlayerFromCharacter(script.Parent)

game.Workspace.Machines.Machine1.Button.ProximityPrompt.Triggered:Connect(function()
	
	if db == false then
		
		game.Workspace.Machines.Machine1.Button.ProximityPrompt.Enabled = false
	
	local ball = game.ReplicatedStorage.Balls.Ball1:Clone()
	
		print("Worked")
	ball.Parent = game.Workspace
	ball.Transparency = 0
	ball.CanCollide = true
	ball.Anchored = false
		
		repeat
			
			wait()
			
		until ball.AssemblyLinearVelocity.Magnitude < .25
		
		ball.Anchored = true
		ball.BrickColor = BrickColor.new(1, 0, 0.0156863)
		
		local parts = ball:GetTouchingParts()  -- here
		for i, part in pairs(parts) do
			if part.Name == "Green" then
				print("Win")
				game.ReplicatedStorage.RemoteEvents.Win:FireServer()
			elseif part.Name == "Red" then
				-- lose code here
			end
		end
				
		wait(1)
		
		ball:Destroy()
		
		db = false
		
		game.Workspace.Machines.Machine1.Button.ProximityPrompt.Enabled = true
		
	end
end)
1 Like

i guess ill try detecting it from the ball instead of having that local script in the part, thanks

for i, part in pairs(parts) do
    print(part.Name) -- this
	if part.Name == "Green" then
	   print("Win")
	   game.ReplicatedStorage.RemoteEvents.Win:FireServer()
	elseif part.Name == "Red" then
	   print("lose")
	end
end

what it showing?

1 Like

Sorry about that, I fell asleep. But yeah, it does print the parts name, which is called “Part” and not “Green” so I guess that was the problem. I’ll try a couple solutions for this and if I have trouble I’ll come back. Thanks!

1 Like

try to make invisible noncollideable parts for zones

Thats what I have now, but it still doesnt seem to be working. Like sometimes it detects a part and sometimes it doesn’t.

Maybe theres something other than GetPartsTouching thatll work? I’m not sure.

(Code is at the end of the vid)

Yeah I guess I was wrong here, the problem wasn’t that the part wasn’t called green, it was that it touched one of the black lines so it detecting that instead of the green part.

1 Like

image
like this?

Yeah thats how it is, but I think I get the problem.

It’s because GetPartsTouching says that it “Gets a table of all BasePart.CanCollide parts that instersect with this part” meaning it’ll detect the part that hit it (like the wall, etc…) and not the part its on top of or the non-collidable zone it’s in.

I still don’t know what to do though.

1 Like

Another solution would be, that you raycast downwards, when the movement of the ball is very low/stopped. You already know the size of the ball, and the size of the floor below. So the raycast length would be -((BallSize.Y/2)+(FloorSize.Y/2))

1 Like

ye, raycast is option too. —

I’ve never done raycasting, could you give an example?

local raycastResult = workspace:Raycast(ball.Position, (ball.Position - (ball.Position + Vector3.new(0, -1, 0))) * 20)
if raycastResult then
   local hitPart = raycastResult.Instance
   print(hitPart.Name)
   if hitPart.Name == "GreenZone" then
	  print("Win")
	  game.ReplicatedStorage.RemoteEvents.Win:FireServer()
   elseif hitPart.Name == "RedZone" then
	  print("Lose")
   end
end

updated first line
put all this code after ball stops
i’l go sleep, 02:28 lol

2 Likes

Oh alr, goodnight man thanks for the help!

If you plan on releasing this model as part of a game, I would highly recommend that you not. ROBLOX has strict TOS rules around gambling and this would certainly count as that.

The TOS says:

" Except where prohibited by local law or regulation, we allow the portrayal of gambling in experiences, no real money, Robux, or anything that can become real money or Robux, may be exchanged in these experiences . We also require that the odds of winning be fair and not skewed in the developer’s favor. "

So I’m guessing as long as it’s not gambling Robux and it’s in-game currency it should be fine.