I’m making a simple mini-game where a player can shoot bullets at ballons to pop them. The idea is simple: When the balloon is Touched, we check if the other part that touched it has a name of bullet, then we also check if they have the same color.
Balloon event handler:
local function onCollision(otherPart, balloon)
print("Collision detected!")
-- check if the other object is a bullet
if otherPart.Name == "Bullet" then
print("Bullet detected!")
-- check if the colors of the balloon and the bullet match
if balloon.Color == otherPart.Color then
-- destroy the balloon
balloon:Destroy()
end
end
end
sphere.Touched:Connect(function(otherPart)
onCollision(otherPart, sphere)
end)
Gun tool:
local gun = script.Parent
Debris = game:GetService("Debris")
local playerChar = gun.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerMouse = player:GetMouse()
gun.Activated:Connect(function()
local bullet = Instance.new("Part")
bullet.Name = "Bullet"
bullet.Size = Vector3.new(1, 1, 1)
bullet.Shape = Enum.PartType.Ball
bullet.Material = Enum.Material.Plastic
bullet.Color = Color3.fromRGB(117, 0, 0)
bullet.Parent = workspace
bullet.CanCollide = true
print("bullet created")
bullet.CFrame = gun.Nozzle.CFrame:ToWorldSpace(CFrame.new(2, 0, 0))
local directionVector = CFrame.lookAt(bullet.Position, playerMouse.Hit.Position).LookVector
local velocityVector = directionVector * 120
bullet:ApplyImpulse(velocityVector)
Debris:AddItem(bullet, 5)
end)
I’m not sure why the balloon’s are not disappearing. The “Bullet detected!” print statement never executes.