[v1.5] Create Dynamic Crosshairs! (Updated!)

I’ve tried that, it does what you expect it would. Crosshair moves down, and the ray shoots at the same position.

Hello, not sure if it’s me doing something wrong, or theres an issue wit the module

I have seperate script that creates the UI and enables it

local UI:ScreenGui = script.Parent 
local DynamicCrosshair = require(game:GetService("ReplicatedStorage").Modules.DynamicCrosshair).New(UI)
DynamicCrosshair:Enable()
--Just like in the tutorial

And then another local script that shoves the croshair when the gun shoots:

local DynamicCrosshair = require(game:GetService("ReplicatedStorage").Modules.DynamicCrosshair)
DynamicCrosshair:Shove()

But for some reason I’m getting this error:

ReplicatedStorage.Modules.DynamicCrosshair:231: attempt to index nil with 'Spread'

If you know the fix please let me know :grinning:

Maybe make a variable that determines if crosshair is changing or not and if its changing then it will cancel the task?

you can do this with simple scripting. just set the position of the gui to the position of the side of the crosshair you want + the offset you want it to be

i cannot help if you dont show the entire script (or atleast the part that uses the crosshair module)

I’m using a spring module for recoil, and for some reason when I use your module along-side the spring module, the recoil is delayed? this is caused when I use the :smoothSet() function???

It somehow delays my whole script even? Nice

task.spawn(function() smooth set func end)

will fix it. SmoothSet function yields until completed

2 Likes

Thank you. (Character limittt)

One little suggestion, you can make 4 directional crosshair for shotguns, although if anyone knows a little bit of scripting they can just edit it to make the 4 directions.

im confused a little bit, can you give a example of what you’re talking about

like the norrmal crosshair that goes to top, bottom, left and right, but this one goes to diagonals, like

1__2
3__4

like if you set z to the same number of y as position will go to diagonal

well, i made an example code:

self.Top, self.Bottom, self.Left, self.Right, self.HitMarker, self.CenterDot, self.ImageCrosshair,self.DiagTRight,self.DiagTLeft,self.DiagBLeft,self.DiagBRight = Functions:CreateHairs(UI, self.SizeX, self.SizeY)
 - line 65 dynamic cross hair module

function Functions:CreateHairs(Parent, X, Y)
	local Hairs 	= {}
		local rot = 0
	for I = 1, 11 do
		local Frame 		= Instance.new("ImageLabel")
		Frame.Image 		= ''
		Frame.Name 			= "_hair"
		Frame.Parent 		= Parent
		Frame.AnchorPoint 	= Vector2.new(0.5, 0.5)
		
		if I < 3 then
			Frame.Size 		= UDim2.fromOffset(Y, X)
		elseif I > 2 and I < 5 then
			Frame.Size 		= UDim2.fromOffset(X, Y)
		elseif I == 5 then
			Frame.BackgroundTransparency	= 1
			Frame.Size 						= UDim2.fromOffset(50, 50)
			Frame.Image 					= 'rbxassetid://285779644'
			Frame.Name 						= "HitMarker"
			Frame.ImageTransparency			= 1
		elseif I == 6 then
			Frame.BackgroundTransparency	= 1
			Frame.Size 						= UDim2.fromOffset(10, 10)
			Frame.Image 					= 'rbxassetid://11003529439'
			Frame.Name 						= "CenterDot"
			Frame.ImageTransparency			= 0
			Frame.Visible 					= false
		elseif I == 7 then
			Frame.BackgroundTransparency	= 1
			Frame.Name 						= 'ImageCrosshair'
			Frame.Visible 					= false
		elseif I > 7 then
			Frame.Size = UDim2.fromOffset(X, Y)
			Frame.BackgroundTransparency	= 1
			Frame.Image = "rbxassetid://18627110127"
			Frame.Rotation = rot
			rot += 90
		end
		
		table.insert(Hairs, I, Frame)
	end
		
	return unpack(Hairs)
end
- on functions module

	self.DiagonalHairs = {
		hairs = {
			self.DiagTRight,
			self.DiagTLeft,
			self.DiagBRight,
			self.DiagBLeft
		},
		enabled = false
	}
 - on crosshairmodule.New()


	self.DiagTRight.Size 				= UDim2.fromOffset(Y, X)
	self.DiagTLeft.Size 			= UDim2.fromOffset(Y, X)
	self.DiagBRight.Size 				= UDim2.fromOffset(X, Y)
	self.DiagBLeft.Size 			= UDim2.fromOffset(X, Y)
 - on CrosshairModule:Size()

and heres how i setted it up:

local DynamicCrosshair = CrosshairModule.New(script.Parent:WaitForChild("ScreenGui"), 0, 99999999, 0, 0, false)
DynamicCrosshair:Enable()

DynamicCrosshair.EasingStyle = Enum.EasingStyle.Linear
DynamicCrosshair.EasingDirection = Enum.EasingDirection.InOut

DynamicCrosshair:FollowMouse(true)

DynamicCrosshair.CenterDot.enabled = true
DynamicCrosshair.CenterDot.Image = "rbxassetid://17787998274"
DynamicCrosshair.CenterDot.size = UDim2.fromOffset(80,80)
DynamicCrosshair.DiagonalHairs.enabled = true
DynamicCrosshair:Size(20,20)

Calling :Destroy() on a crosshair object doesn’t destroy the hitMarker. I fixed this myself by adding self.HitMarker.hitmarker:Destroy() in the :destroy() method.

1 Like