Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does the part not spawn where it should?

I made a script for a bullet whenever it touches an object it deletes itself and is replaced by a black part stuck inside whatever the bullet hit only problem is the part goes higher than where the bullet hit on the wall

**Here's my script : **

script.Parent.Touched:connect(function(hit)
	if hit.Parent.Humanoid then
		hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health -10
	end
end)
--beyond this point is an error be careful!
script.Parent.Touched:connect(function(hit)
	local p = Instance.new("Part", workspace)
	p.Position = script.Parent.Position
	p.FormFactor = "Custom"
	p.Size = Vector3.new(0.2, 0.2, 0.2)
	p.Color = Color3.new(0, 0, 0)
p.CFrame = p.CFrame - Vector3.new(.2, -4, .2)
	p.Anchored = true

	script.Parent:Destroy()
end)

1 answer

✓ Accepted answer

Your error is on line 13. When you create a block it's orientation is the same as the world axis. So when you subtract -4 (same as adding 4) from the y-axis the part will be positioned 4 studs above the position you started with.

A better way to do it would be to change the part's CFrame to the bullet's CFrame so they both have the same orientation. You can then add on the look vector of the bullet, assuming it faces the direction it fires.

p.CFrame = p.CFrame + p.CFrame.lookVector * 0.195 -- Just Sticking out of the block

This discussion is preserved as it was. New answers, comments, and votes are closed.