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

How would I prevent the brick from going around the block it is placed?

Asked by 7 years ago

The brick, "LightExplosion" is a ball part that is fired with a laser and then rapidly expands to its size. The issue is when it resizes it goes around blocks instead of cframing into them and it only happens with the onTouch function. When the function is removed it works fine. How would I fix the function to where it doesn't force the ball to go over the designated part and instead half into it? Here's the code causing the problem:

-- Adds a repulsive force to anything this object touches

local Debris = game:GetService('Debris')


local CharacterToIgnore = script:WaitForChild('CharacterToIgnore').Value



local MAGNITUDE = 3E4

local TIME_OF_FORCE = 0.2


local Part = script.Parent
Part.Touched:connect(function(other)
    Part.CanCollide= false
    Part.Anchored = true

 --Variable goes here!

    if other.Parent == CharacterToIgnore or (other.Parent and other.Parent.Parent == CharacterToIgnore) then return end

    if not other.Anchored then

       -- local punchSound = script:FindFirstChild('PunchSound')

        --if punchSound then punchSound:Play() end

        local direction = (other.Position - Part.Position).unit

        local bodyForce = Instance.new('BodyForce')

        bodyForce.force = MAGNITUDE * direction-- Adds a repulsive force to anything this object touches

local Debris = game:GetService('Debris')


local CharacterToIgnore = script:WaitForChild('CharacterToIgnore').Value



local MAGNITUDE = 3E4

local TIME_OF_FORCE = 0.2


local Part = script.Parent
Part.Touched:connect(function(other)
    Part.CanCollide= false
    Part.Anchored = true

 --Variable goes here!

    if other.Parent == CharacterToIgnore or (other.Parent and other.Parent.Parent == CharacterToIgnore) then return end

    if not other.Anchored then

       -- local punchSound = script:FindFirstChild('PunchSound')

        --if punchSound then punchSound:Play() end

        local direction = (other.Position - Part.Position).unit

        local bodyForce = Instance.new('BodyForce')

        bodyForce.force = MAGNITUDE * direction

        bodyForce.Parent = other

        Debris:AddItem(bodyForce, TIME_OF_FORCE)

    end

end)


        bodyForce.Parent = other

        Debris:AddItem(bodyForce, TIME_OF_FORCE)

    end

end)

Here's the code where the ball is actually created:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

local beam = Instance.new("Part", workspace)


tool.Equipped:connect(function(mouse)
    print("Tool equipped!")
    --wait(1)
    mouse.Button1Down:connect(function()

        script.Disabled = true
        script.Parent.Sound:Play()
        print("READY A")
        print("Mouse pressed!")

        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)


        beam.BrickColor = BrickColor.Yellow()
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false
        beam.Shape = "Ball"
        beam.Name = "LightExplosion"
        beam.Anchored = true








        local distance = (tool.Handle.CFrame.p - position).magnitude
        beam.Size = Vector3.new(1, 1, 1)
        beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 1)
        beam.Size = Vector3.new(2, 2, 2)
    local scriptCopy = game.ReplicatedStorage.RepelScript:Clone()
        scriptCopy.Parent = game.Workspace.LightExplosion
    beam.BrickColor = BrickColor.new("Orange")
        wait(.05)
    beam.Size = Vector3.new(3, 3, 3)
    beam.BrickColor = BrickColor.new("Orange")
    wait(.05)
    beam.Size = Vector3.new(4, 4, 4)
    beam.BrickColor = BrickColor.new("Orange")
    wait(.05)
    beam.Size = Vector3.new(5, 5, 5)
    beam.BrickColor = BrickColor.Yellow()
    wait(.05)
    beam.Size = Vector3.new(6, 6, 6)
    beam.BrickColor = BrickColor.new("Orange")
    wait(.05)-- 
    beam.Size = Vector3.new(7, 7, 7)
    beam.BrickColor = BrickColor.Yellow()
    wait(.05)
    beam.Size = Vector3.new(8, 8, 8)
    beam.BrickColor = BrickColor.new("Orange")
    wait(.05)
    beam.Size = Vector3.new(9, 9, 9)
    beam.BrickColor = BrickColor.Yellow()
    wait(.05)
    beam.Size = Vector3.new(10, 10, 10)
    beam.BrickColor = BrickColor.new("Orange")
    wait(.05)
    beam.Size = Vector3.new(11, 11, 11)
    beam.BrickColor = BrickColor.Yellow()
    wait(.05)
    beam.Size = Vector3.new(12, 12, 12)
    beam.BrickColor = BrickColor.new("Orange")
    wait(.05)
    beam.Size = Vector3.new(13, 13, 13)
    beam.BrickColor = BrickColor.Yellow()
    wait(.05)
    beam.Size = Vector3.new(14, 14, 14)
    beam.BrickColor = BrickColor.new("Orange")
    wait(.05)
    beam.Size = Vector3.new(15, 15, 15)
    beam.BrickColor = BrickColor.Yellow()





        game:GetService("Debris"):AddItem(beam, 1)
        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")

            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end

            if humanoid then
                humanoid:TakeDamage(30)
            end
            script.Parent.Reload.Disabled = false


        end

    end)
end)

--script.Parent.Reload.Disabled = false
        --script.Disabled = true


Answer this question