Gun shot max distance

how can i add max distance to my gun?
because i dont want people to shot from a infinite max distance
you can also improve this code if it looks bad
because i am not a PRO at coding

local Gun = script.Parent
local BloodEffect = game.ServerStorage.BloodEffect
local BulletImpact = game.ServerStorage.BulletImpact

Gun.ShootEvent.OnServerEvent:Connect(function(player, mouseHit)
	Gun.Enabled = false
	if Gun.Ammo.Value <= 0 then
		Gun.Handle.ClickSound:Play()
	end
	if Gun.Ammo.Value > 0 then
		local Params = RaycastParams.new()
		local Direction = (mouseHit - Gun.Handle.GunTip.WorldPosition).Unit * 1000000000000000
		Params.FilterDescendantsInstances = Gun.Parent:GetDescendants()
		Params.FilterType = Enum.RaycastFilterType.Exclude
		local Raycast = workspace:Raycast(Gun.Handle.GunTip.WorldPosition, Direction, Params)	
		if Raycast ~= nil then
			if Raycast.Instance ~= nil then
				local CloneImpact = BulletImpact:Clone()
				CloneImpact.Parent = workspace
				CloneImpact.Position = Raycast.Position
				if Raycast.Instance.Name == "Window" then
					Raycast.Instance.Transparency = 1
				end
				if Raycast.Instance.Parent:FindFirstChildWhichIsA("Humanoid") then
					local CloneEffect = BloodEffect:Clone()
					CloneEffect.Parent = Raycast.Instance
					Raycast.Instance.Parent:FindFirstChildWhichIsA("Humanoid"):TakeDamage(math.random(20, 32))
				end
			end
		end
		Gun.Ammo.Value -= 1
		Gun.Handle.ShootSound:Play()
		task.wait(.6)
	end
	Gun.Enabled = true
end)

Gun.ReloadEvent.OnServerEvent:Connect(function(player)
	Gun.Enabled = false
	repeat 
		if player.Backpack:FindFirstChild("AmmoFolder")[".44MagnumRounds"].Value > 0 then
			player.Backpack:FindFirstChild("AmmoFolder")[".44MagnumRounds"].Value -= 1
			Gun.Ammo.Value += 1
		end
	until player.Backpack:FindFirstChild("AmmoFolder")[".44MagnumRounds"].Value == 0 or Gun.Ammo.Value == Gun.MaxAmmo.Value
	Gun.Handle.ReloadSound:Play()
	task.wait(2.4)
	Gun.Handle.BoltSound:Play()
	Gun.Enabled = true
end)

multiply it by a smaller amount than one trillion, something like 1000 might work.

thank you, i followed a tutorial on youtube on how to make a raycast gun and i didint know how to change the distance.

2 Likes