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

How to make a script check for a collision?

Asked by 10 years ago

How can I get a script, like a gun, when it shoots a wall, the bullet detects the collision and deletes it self. All I am asking what type of coding do I need to check for a collsion with anything named "Brick"??

Gun Script (Only got the main parts youll need to see)

    local missile = Instance.new("Part")

    local spawnPos = vCharacter.PrimaryPart.Position

    spawnPos  = spawnPos + (v * 10)

    missile.Position = spawnPos
    missile.Size = Vector3.new(1,1,1)
    missile.Velocity = v * 300
    missile.BrickColor = BrickColor.new(26)
    missile.Shape = 0
    missile.BottomSurface = 0
    missile.TopSurface = 0
    missile.Material = Enum.Material.Plastic
    missile.Elasticity = .05
    missile.Friction = .7
    missile.Name = "Bullet"
    missile.CanCollide = true

I thought I would just add the whole gun script. Sorry if it is long. Most of it is just variables, probably go down to line 100 around their would be the shooting part

local Tool = script.Parent;
local reloading = 0

enabled = true

local reloadSound = Instance.new("Sound")
reloadSound.Name = "reloadSound"
reloadSound.SoundId = "http://www.roblox.com/asset/?id=13510737"
reloadSound.Parent = Tool.Handle
reloadSound.Volume = 2

local reloadSound2 = Instance.new("Sound")
reloadSound2.Name = "reloadSound2"
reloadSound2.SoundId = "http://www.roblox.com/asset/?id=25299064"
reloadSound2.Parent = Tool.Handle
reloadSound2.Volume = 2

function fire(v)

    local vCharacter = Tool.Parent
    local vPlayer = game.Players:playerFromCharacter(vCharacter)



    local noise = Vector3.new(math.random() - .3, math.random() - .3, math.random() - .3).unit

    v = (v + (noise * .03)).unit

    local missile = Instance.new("Part")

    local spawnPos = vCharacter.PrimaryPart.Position

    local PewPew = Tool.Handle:FindFirstChild("PewPew")

    if (PewPew == nil) then
        PewPew = Instance.new("Sound")
        PewPew.Name = "PewPew"
        PewPew.SoundId = "http://www.roblox.com/asset/?id=13510352"
        PewPew.Parent = Tool.Handle
        PewPew.Pitch = .5
        PewPew.Volume = 10
    end

    spawnPos  = spawnPos + (v * 10)

    missile.Position = spawnPos
    missile.Size = Vector3.new(1,1,1)
    missile.Velocity = v * 900
    missile.BrickColor = BrickColor.new(26)
    missile.Shape = 0
    missile.BottomSurface = 0
    missile.TopSurface = 0
    missile.Material = Enum.Material.Plastic
    missile.Elasticity = .05
    missile.Friction = .7
    missile.Name = "Bullet"
    missile.CanCollide = true
    missile.Transparency = 1

    local force = Instance.new("BodyForce")
    force.Name = "BulletFloat"
    force.force = Vector3.new(0,99,0)
    force.Parent = missile

    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = vPlayer
    creator_tag.Name = "creator"
    creator_tag.Parent = missile

    local new_script = script.Parent.LaserBlast:clone()
    new_script.Disabled = false
    new_script.Parent = missile

    missile.Parent = game.Workspace

    PewPew:Play()

end

function gunUp()
    Tool.GripForward = Vector3.new(0,.981,-.196)
    Tool.GripRight = Vector3.new(1,0,0)
    Tool.GripUp = Vector3.new(0,.196,.981)
    script.Parent.Fire.Fire.Enabled = true 
    script.Parent.Fire.LSmoke.Enabled = true
    script.Parent.Fire.Smoke.Enabled = true
    wait(0.000000000000000000000000000000000001)
    script.Parent.Fire.Fire.Enabled = false
    script.Parent.Fire.LSmoke.Enabled = false
end

function gunOut()
    Tool.GripForward = Vector3.new(0,1,0)
    Tool.GripRight = Vector3.new(1,0,0)
    Tool.GripUp = Vector3.new(0,0,1)    
    script.Parent.Fire.Smoke.Enabled = false

end

function isTurbo(character)
    return character:FindFirstChild("BoltHelm") ~= nil
end


function onActivated()
    if not enabled  then
        return
    end

    enabled = false

    local character = Tool.Parent;
    local humanoid = character.Humanoid
    if humanoid == nil then
        print("Humanoid not found")
        return 
    end

    local targetPos = humanoid.TargetPoint
    local lookAt = (targetPos - character.Head.Position).unit

    local reload = 2

    gunUp()
    fire(lookAt)

    reloading = reloading + 1
    if reloading >= 6 then
        if reloadSound ~= nil then
            wait(0.1)
            Tool.GripUp = Vector3.new(0,0,1)
            wait(0.1)
            reloadSound:Play()
        end
        reloading = 0
        Tool.GripUp = Vector3.new(0,0,1)
        wait(0.1)
        Tool.GripUp = Vector3.new(0,-0.2,1)
        wait(0.1)
        Tool.GripUp = Vector3.new(0,-0.5,1)
        wait(0.3)
        reloadSound2:Play()
        wait(0.3)
        reloadSound2:Play()
        wait(0.3)
        reloadSound2:Play()
        wait(0.3)
        reloadSound2:Play()
        wait(0.3)
        reloadSound2:Play()
        wait(0.3)
        reloadSound2:Play()
        wait(0.3)
        reloadSound:Play()
        wait(0.1)
        Tool.GripUp = Vector3.new(0,-0.2,1)
        wait(0.1)
        Tool.GripUp = Vector3.new(0,0,1)
    end

    wait(reload)
    gunOut()
    wait(reload)

    enabled = true

end

function onEquipped()
    Tool.Handle.EquipSound:play()
end

script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)

1 answer

Log in to vote
0
Answered by 10 years ago

You could do,

missile.Touched:connect(function(hit)
    if hit.Parent.Name == "Brick" then
        missile:Destroy()
    end
end)

or if you just want any part do

missile.Touched:connect(function(hit)
    missile:Destroy()
end)
0
Dont work. I need it so when a bullet hits a part called Brick, it destroys itself. Does the bullet need to be CanCollide? NinjoOnline 1146 — 10y
0
Show me the script you made ROBLOXING213 130 — 10y
0
Ok editted NinjoOnline 1146 — 10y
0
Edited ROBLOXING213 130 — 10y
View all comments (3 more)
0
dosent shoot now. Where should the function be placed? I dont wanna paste the whole script, cause its like 160 lines NinjoOnline 1146 — 10y
0
Should be placed in the function when it shoots ROBLOXING213 130 — 10y
0
i didnt script the gun, so I don't know where the firing part is? NinjoOnline 1146 — 10y
Ad

Answer this question