Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does the part not spawn where it should?

Asked by
Prioxis 673 Moderation Voter
10 years ago

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

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
10 years ago

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
Ad

Answer this question