[Open Source] Impactframe Module

ImpactEffect Module

Overview

This module provides functionality to create customizable impact effects in your games. It allows you to apply color correction effects to specified instances upon impact, creating visually impactful moments in gameplay.

Usage

  1. Services

    • Lighting: Required for applying color correction effects.
    • HttpService: Used for generating unique IDs.
  2. Variables

    • PlaybackState: Enum defining playback states.
    • ImpactFrames: Array storing created impact effect instances.
  3. Functions

  • Module:Create(Settings: {HitType: "White"|"Black"|"Custom", HitList: {Instance}, CustomSettings: {Color: Color3?, Saturation: number?, Contrast: number?}?}, Duration: number?, PlayOnce: boolean?): any: Creates a new impact effect instance with customizable settings. Optionally, it plays the effect once upon creation.
    • Arguments:
      HitType : "White" or "Black" or "Custom"
      Defines the color of the instances in HitList when the ImpactFrame is played.

      HitList : {Instance}
      The list of Instances you want to change during the ImpactFrame.

      CustomProperties : {TintColor: Color3?, Saturation: number?, Contrast: number?}?
      Custom Properties for the ColorCorrectionEffect if HitType is "Custom".

      Duration : number? or 0.1
      Duration of the ImpactFrame

      PlayOnce: boolean? or false
      Plays once before getting removed.
  1. ImpactEffect Instance
    • Properties:
      • State: Playback state of the effect.
      • UniqueID: Unique identifier for the effect instance.
      • Instance: ColorCorrectionEffect instance applied to the lighting.
    • Methods:
      • Play(Destroy: boolean): Plays the impact effect. Optionally destroys the effect instance after playing.
      • Stop(): Stops the impact effect.
      • Destroy(): Destroys the impact effect instance.
      • Pause(): Pauses the impact effect.
      • Resume(): Resumes the paused impact effect.

Example Usage

local ImpactEffect = require(game.ServerScriptService.ImpactEffectModule)

-- Create impact effect with custom settings
local effect = ImpactEffect:Create({
    HitType = "Custom",
    HitList = {workspace.Part},
    CustomSettings = {
        Color = Color3.new(1, 0, 0),
        Saturation = -0.5,
        Contrast = 5000
    },
    Duration = 0.2
})

effect:Play()

Notes

  • Handle impact effect instances carefully to avoid memory leaks or unexpected behavior.
  • Experiment with different settings to achieve desired visual effects.

Get the Module here

Source
--// Services //--
local Lighting = game:GetService("Lighting")
local HttpService = game:GetService('HttpService')

--//Variables //--
local PlaybackState = Enum.PlaybackState
local Module = {
	ImpactFrames = {}
}

--// Functions //--

function ChangeInstances(_Instances:{Instance}?,ToNormal:{Yes:boolean?,Default:{[any]:any}?},Settings:{Color:Color3,Highlight:boolean?}?): any
	local DefaultSettings = {}

	if ToNormal.Yes and ToNormal.Default then
		for _Instance, Property in ToNormal.Default do
			if _Instance:IsA('BasePart') then
				_Instance.Material = Property.Material
				_Instance.Color = Property.Color
			elseif _Instance:IsA('Decal') then
				_Instance.Transparency = Property.Transparency
			end
			if _Instance:FindFirstChild('Highlight') then
				_Instance.Highlight:Destroy()
			end
		end
		return
	end
	
	if not _Instances or not Settings then return end
	local function ApplyToBasePart(BasePart:BasePart)
		DefaultSettings[BasePart] = {}
		DefaultSettings[BasePart].Material = BasePart.Material
		DefaultSettings[BasePart].Color = BasePart.Color
		BasePart.Material = Enum.Material.Neon
		BasePart.Color = Settings.Color
	end
	
	for _, InstanceToSearch in _Instances do
		for _, _Instance in InstanceToSearch:GetDescendants() do
			if _Instance:IsA('BasePart') then
				ApplyToBasePart(_Instance)
			elseif _Instance:IsA('Decal') then
				DefaultSettings[_Instance] = {Transparency = _Instance.Transparency}
				_Instance.Transparency = 1
			end
			if Settings.Highlight then
				local Highlight = Instance.new('Highlight')
				Highlight.FillTransparency = 0
				Highlight.OutlineTransparency = 0
				Highlight.OutlineColor = Settings.Color
				Highlight.Parent = _Instance
			end
		end
	end
	return DefaultSettings
end

function Module:Create(Settings:{HitType:"White"|"Black"|"Custom",HitList:{Instance},CustomProperties:{TintColor:Color3?,Saturation:number?,Contrast:number?}?},Duration:number?,PlayOnce:boolean?): any
	local UniqueID = HttpService:GenerateGUID(false)
	local TimeStamps = {Started = 0,Ended = 0}
	local Defaults
	local ImpactFrame
	
	ImpactFrame = {State = PlaybackState.Paused,UniqueID=UniqueID}
	
	--//[ Effect Setup ] \\--
	local EffectSettings = {}
	if Settings.HitType == 'White' then
		EffectSettings = {
			Saturation = -1,
			Contrast = 25000,
			TintColor = Color3.new(0.639216, 0.635294, 0.647059),
		}
	elseif Settings.HitType == 'Black' then
		EffectSettings = {
			Saturation = -1,
			Contrast = -25000,
			TintColor = Color3.new(0.639216, 0.635294, 0.647059),
		}
	elseif Settings.HitType == 'Custom' and Settings.CustomProperties then
		EffectSettings = {
			Saturation = Settings.CustomProperties.Saturation or -1,
			Contrast = Settings.CustomProperties.Contrast or 25000,
			TintColor = Settings.CustomProperties.TintColor or Color3.new(1,1,1),
		}
	end
	Duration = Duration or 0.1

	--//[ ColorCorrection Instancing ] \\--
	local ColorCorrectionEffect = Instance.new('ColorCorrectionEffect')
	ColorCorrectionEffect.Name = `ImpactEffect | {UniqueID}`
	for Property, Value in EffectSettings do
		ColorCorrectionEffect[Property] = Value
	end
	ImpactFrame.Instance = ColorCorrectionEffect

	--//[ Effects Functions ] \\--
	function ImpactFrame:Play(Destoy)
		if ImpactFrame.State == PlaybackState.Playing then return '[ERROR]: Already Playing' end
		ImpactFrame.State = PlaybackState.Playing
		ColorCorrectionEffect.Parent = Lighting
		TimeStamps.Started = os.clock()
		Defaults = ChangeInstances(Settings.HitList,{},{Color = EffectSettings.TintColor})
		task.delay(Duration,function()
			if ImpactFrame.State == PlaybackState.Playing then  
				ChangeInstances({},{Yes=true,Default=Defaults})
				if Destoy then
					ColorCorrectionEffect:Destroy()
					table.clear(ImpactFrame)
					table.clear(Defaults)
					Defaults = nil
					ImpactFrame = nil
					return
				end
				ColorCorrectionEffect.Parent = nil
				ImpactFrame.State = PlaybackState.Completed
			end
		end)
		return 'Played'
	end
	if PlayOnce then ImpactFrame:Play(true) return end
	
	function ImpactFrame:Stop()
		if ImpactFrame.State ~= PlaybackState.Playing then return '[ERROR]: Not currently Playing' end
		ColorCorrectionEffect.Parent = nil
		ImpactFrame.State = PlaybackState.Cancelled
		ChangeInstances({},{Yes=true,Default=Defaults})
	end
	function ImpactFrame:Destroy()
		ImpactFrame:Stop()
		ColorCorrectionEffect:Destroy()
		table.clear(ImpactFrame)
		table.clear(Defaults)
		Defaults = nil
		ImpactFrame = nil
	end

	--// 2nd states //--
	function ImpactFrame:Pause()
		if ImpactFrame.State ~= PlaybackState.Playing then return '[ERROR]: Not currently Playing' end
		ImpactFrame.State = PlaybackState.Paused
		TimeStamps.Ended = os.clock()
		return 'Paused'
	end
	function ImpactFrame:Resume()
		if ImpactFrame.State ~= PlaybackState.Paused then return '[ERROR]: Not currently Paused' end
		ImpactFrame.State = PlaybackState.Playing
		local RemainingTime = Duration - (TimeStamps.Ended - TimeStamps.Started)
		ColorCorrectionEffect.Parent = Lighting
		task.delay(RemainingTime, function()
			if ImpactFrame.State == PlaybackState.Playing then  
				ColorCorrectionEffect.Parent = nil
				ImpactFrame.State = PlaybackState.Completed
			end
		end)
		return 'Resumed'
	end

	--//[ Result ] \\--
	table.insert(Module.ImpactFrames,ImpactFrame)
	return ImpactFrame
end

return Module

This is my first Open Source, I made this module in 24 hours out of boredom.
If you find a bug, or you have a suggestion for the module, leave a reply below and I’ll add or fix it.

I’m working on adding highlights instead of using neons, but this is not possible at the moment due to highlights not being supported on mobile devices.

have fun

  • ParadoxAssets
10 Likes

An example of this module would be appreciated.

9 Likes