Part's .Name not being recorded properly?

I am trying to make a collision detection for a mechanism where we can make balloons pop by shooting from a simple gun. We do this by first checking that the bullet’s name is “Bullet”, and if the balloon and the bullet are the same color we will pop the gun. Here is the script for the 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)

And here is the onCollision function for the balloon:

local function onCollision(otherPart, balloon)
	print("Collision detected!")
	print(otherPart.Name)
	print(otherPart.Color)
	-- 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

When printing out the print statements, here is what I got:

image

As you can see, the bullet’s name was recorded as “Part” instead of “Bullet” which we programmatically changed in the tool. What is happening here?

First of all, can you show the script where you run onCollision? There’s no clue if you don’t show the origin.

Here’s the full script:

local collectionService = game:GetService("CollectionService")
local tweenService = game:GetService("TweenService")

local minX = 80.25
local minY = 5.75
local minZ = -49.25
local maxX = 103.75
local maxY = 17.25
local maxZ = -49.25


local function onCollision(otherPart, balloon)
	print("Collision detected!")
	print(otherPart.Name)
	print(otherPart.Color)
	-- 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


local function generateBalloons(count)
	for i = 1, count do
		-- Create a new sphere
		local sphere = Instance.new("Part")
		sphere.Anchored = true
		sphere.Size = Vector3.new(2, 1, 1)
		sphere.Parent = game.Workspace
		sphere.Shape = Enum.PartType.Ball
		sphere.Material = Enum.Material.SmoothPlastic
		sphere.Color = Color3.fromRGB(117, 0, 0)
		sphere.CanTouch = true
		sphere.CanCollide = true
		sphere.Name = "Sphere"
		--Set event handler for collision with bullets
		sphere.Touched:Connect(function(otherPart)
			onCollision(otherPart, sphere)
		end)
		
		-- Set the position of the sphere
		sphere.Position = Vector3.new(92, 13, -49.25)
		collectionService:AddTag(sphere, "MovingSphere")
	end
end

generateBalloons(10)

local function applyRandomMovement(part)
	-- create a TweenInfo object to control the tween
	local tweenInfo = TweenInfo.new(
		1, -- duration of the tween (in seconds)
		Enum.EasingStyle.Linear, -- easing style
		Enum.EasingDirection.Out, -- easing direction
		0, -- number of times the tween should repeat
		false, -- should the tween be reversed?
		0 -- delay before starting the tween (in seconds)
	)
	
	-- calculate a random target position for the balloon
	local targetPosition = Vector3.new(
		math.random(minX, maxX),
		math.random(minY, maxY),
		math.random(minZ, maxZ)
	)
	-- create a Tween object to move the balloon
	local tween = tweenService:Create(part, tweenInfo, {Position=targetPosition})

	-- start the tween
	tween:Play()
end

local function updateSpheres()
	local spheres = collectionService:GetTagged("MovingSphere")
	
	for _, sphere in pairs(spheres) do
		spawn(function()
			applyRandomMovement(sphere)
		end)
	end
	-- call this function again to keep the balloon moving
	wait(1)
	updateSpheres()
end

updateSpheres()


I’m wondering if this is related to a Server/Client issue. The bullet script is a LocalScript in the gun tool and this balloon script is inside ServerScriptService.

The only thing you can do is making the Bullet inside ServerStorage and cloning it.

So why is this the solution and why is the current way not allowed/leading to the behaviour I showed?

1 Like

Maybe the problem is making so many changes in a new instance.

This also includes the ballons*.