How to make part collide correctly? collision not working

I have a tool that clones the part I’m holding and shoots it out towards my cursor.

The issue is that the part just phases through everything, any object, and the ground.

Every object I use has CanCollide and CanTouch enabled, and so does the part I’m using for the tool, so I don’t understand what can possibly be causing this.

My only thought is the script the tool uses to shoot, so if anyone can find the issue, or fix the script to make the collision work, let me know.

local Tool = script.Parent
local Remote = Tool:WaitForChild("Remote")
local Handle = Tool:WaitForChild("Handle")

local FriendlyFire = false

local AttackPower = 1
local AttackDamage = 22.5
local AtackRechargeTime = 0.1
local AttackRecharge = 50/AtackRechargeTime
local AttackSpeed = 200

local Equipped = false
local Heartbeat = game:GetService("RunService").Heartbeat

local Knives = {}

--Returns the wielding player of this tool
function getPlayer()
	local char = Tool.Parent
	return game:GetService("Players"):GetPlayerFromCharacter(char)
end

--helpfully checks a table for a specific value
function contains(t, v)
	for _, val in pairs(t) do
		if val == v then
			return true
		end
	end
	return false
end

--Tags a human for the ROBLOX KO system
function tagHuman(human)
	local tag = Instance.new("ObjectValue")
	tag.Value = getPlayer()
	tag.Name = "creator"
	tag.Parent = human
	game:GetService("Debris"):AddItem(tag)
end

--Used by checkTeams
function sameTeam(otherHuman)
	local player = getPlayer()
	local otherPlayer = game:GetService("Players"):GetPlayerFromCharacter(otherHuman.Parent)
	if player and otherPlayer then
		if otherPlayer.Neutral then
			return false
		end
		return player.TeamColor == otherPlayer.TeamColor
	end
	return false
end

--Determines if you want this human to be harmed or not, returns boolean
function checkTeams(otherHuman)
	return not (sameTeam(otherHuman) and not FriendlyFire)
end

function getKnife()
	local knife = Handle:clone()
	knife.Transparency = 0
	knife.Hit.Pitch = math.random(90, 110)/100
	
	local lift = Instance.new("BodyForce")
	lift.force = Vector3.new(0, 196.2, 0) * knife:GetMass() * 0.4
	lift.Parent = knife
	
	local proj = Tool.Projectile:Clone()
	proj.Disabled = false
	proj.Parent = knife
	
	return knife
end

function equippedLoop()
	while Equipped do
		local dt = Heartbeat:wait()
		
		if AttackPower < 1 then
			AttackPower = AttackPower + dt * AttackRecharge
			if AttackPower > 1 then
				AttackPower = 1
			end
		end
		
		Handle.Transparency = 1 - AttackPower
	end
end

function onLeftDown(mousePos)
	local knife = getKnife()
	knife.CFrame = CFrame.new(Handle.Position, mousePos)
	knife.Velocity = knife.CFrame.lookVector * AttackSpeed * AttackPower
	local damage = AttackDamage * AttackPower
	local touched
	touched = knife.Touched:connect(function(part)
		if part:IsDescendantOf(Tool.Parent) then return end
		if contains(Knives, part) then return end
		
		if part.Parent and part.Parent:FindFirstChild("Humanoid") then
			local human = part.Parent.Humanoid
			if checkTeams(human) then
				tagHuman(human)
				human:TakeDamage(damage)
				knife.Hit:Play()
			end
		end
		
		touched:disconnect()
	end)
	table.insert(Knives, knife)
	knife.Parent = workspace
	
	game:GetService("Debris"):AddItem(knife, 6)
	delay(2, function()
		knife.Transparency = 1
	end)
	
	Remote:FireClient(getPlayer(), "PlayAnimation", "Throw")	
	
	Handle.Throw.Pitch = 0.8 + 0.4 * AttackPower
	Handle.Throw:Play()
	
	AttackPower = 0
end

function onRemote(player, func, ...)
	if player ~= getPlayer() then return end
	
	if func == "LeftDown" then
		onLeftDown(...)
	end
end

function onEquip()
	Equipped = true
	equippedLoop()
end

function onUnequip()
	Equipped = false
end

Remote.OnServerEvent:connect(onRemote)
Tool.Equipped:connect(onEquip)
Tool.Unequipped:connect(onUnequip)

Does your Projectile script set CanCollide to false?

This is the “Projectile” script:

local Projectile = script.Parent
local Heartbeat = game:GetService("RunService").Heartbeat

while Heartbeat:wait() do
	Projectile.CFrame = CFrame.new(Projectile.Position, Projectile.Position + Projectile.Velocity)
end

I think it might be setting a new position depending on how it should look, which is why it’s going through objects.

Yeah you projectile acript means the knife will go infinity forward. Unless ypu set the velocity to 0 after it touches something

1 Like

I removed the projectile script and the script that re-enables it, but it still goes through things. Not sure what to do from here.

What does handle:play() do
Also is the touched function printing anything?

Cannot find where you see “handle:play()” neither do I know what it does.

Also, I brought back the script components that cause the projectile to stick to the ground like it normally did, and it didn’t print anything. Neither did it print anything even without it.

Handle.Throw:play()

extra words so i can reply

There’s a sound attached to the tool called “Throw”
That script plays it when I click.

Where is the code for tool.activated, i see onLeftDown() but i dont see userinputbegan
Nvm i do

Ok firgured it out. You anchored the projectile right? Touched events require that at least one of the objects be unachored

The projectile is unanchored at the moment.

And the touched event still doesnt print anything? Make sue the print is the first statement after the event

There isn’t a function that prints a specific message anywhere, and neither does it automatically do that.

Its for debugging the first step to finding the problem is finding where the problem comes from

That’s the issue, there never is a problem shown on output or anything.

Did you put the print statement

I don’t know where I should put it.

forgot to mention I don’t script at all, if that was the goal here

Oh yikes thay why you didnt k ow about debugging, i really cant help you at this point unless we do like discord screen share. Cause it will take too long to explain everything to do in text

Yeah, that would be easier.

We can do that tomorrow probably.

vvorttexx#5654 (add this for now)

1 Like